Groups | Blog | Home
all groups > inetserver asp general > august 2003 >

inetserver asp general : identify duplicates in an array and number of times duplicated


Ray at <%=sLocation%
8/1/2003 10:17:04 AM
If you've already sorted your array alphabetically, you can do:

Dim sCurVal, sLastVal, iDups
iDups = 0
For i = LBound(YourArray) To UBound(YourArray)
sCurVal = YourArray(i)
If sCurVal = sLastVal Then iDups = iDups + 1
sLastVal = sCurVal
Next


This will just give you the total number of duplicate items, like, if your
array is

a,b,c,c,d,e,e,e,f,g

iDups will return as 3, since there is one extra c and two extra e's.

Is that what you wanted, or did you want to be able to say:
c has 1 duplicate
e has two duplicates
etc.

Ray at work





[quoted text, click to view]

dlbjr
8/1/2003 5:06:28 PM
Why not do a count in the SQL query string and get the username and count from the recordset?



-------------------------------------------------
d l b j r

Unambit from meager knowledge of inane others,
engender uncharted sagacity.
-------------------------------------------------

Michelle
8/2/2003 12:09:29 AM
hi,

i have created an array from recordset containing user names
eg. (davidp, davidp, evenf, patricka, rebeccah)

which i have sorted in alphabetical order, but i need to
identify duplicates in this array and the number of times it has
been duplicated.

can someone help?

--
Michelle


Chris Barber
8/2/2003 12:11:56 AM
You'd be better off getting another recordset with the duplicate counts
already created for you?

SELECT [Name] as UserName, Count([Name]) as CountOfUserName
FROM YourTable
GROUP BY [Name]
ORDER BY Count([Name]) DESC

This will give you:

UserName CountOfUserName
Chris 10
Dave 7
Henrik 2
Michale 1
Joan 1

etc.

If you only want the duplicates listed then change it to be:

SELECT [Name] as UserName, Count([Name]) as CountOfUserName
FROM YourTable
WHERE Count([Name]) > 1
GROUP BY [Name]
ORDER BY Count([Name]) DESC

Hope this helps.

Chris.

[quoted text, click to view]
hi,

i have created an array from recordset containing user names
eg. (davidp, davidp, evenf, patricka, rebeccah)

which i have sorted in alphabetical order, but i need to
identify duplicates in this array and the number of times it has
been duplicated.

can someone help?

--
Michelle



Michelle
8/2/2003 11:46:03 AM
Hi Ray

[quoted text, click to view]

Yes this is what i want

I tried to use your code, although i had to add 'end if', it gave me a
a 'Subscript out of range' error at
[quoted text, click to view]

----------------------------------------------
this is my code for the array
----------------------------------------------
<%
Dim MyArray, sOutput
MyArray = rs.GetRows()
MyArray = arraysort(MyArray)

Dim iRowLoop
For iRowLoop = 0 to UBound(MyArray, 2)
sOutput = trim(left(MyArray(iColLoop, iRowLoop),10)) & "<br> "
Response.Write(sOutput)
Next
%>
----------------------------------------------

Actually the rows in the array is unique, the source is a text file dump of
terminal service processes, but i need to extract the user names of those
using a particular process twice, and how many times they have these
processes open. I cannot sort or search for duplicates in SQL, because the
first line contains a lot of spaces inbetween the headings which dosnt make
a qualified column name.

Hope you can help.

--
Michelle


[quoted text, click to view]

Michelle
8/2/2003 5:23:35 PM
Hi All

Thank you for your inputs, but i have solved my problem,
i didnt ask the right question in the beginning, what i needed was
to search a string for duplicates and the number of times the value
had been duplicated.

But what i had was, an array of unique values, which i manipulated
and displayed to show some duplicates.

Thanks again.
--
Michelle


[quoted text, click to view]

AddThis Social Bookmark Button