Forum Moderators: open

Message Too Old, No Replies

Remove duplicates from array?

         

erikcw

8:14 pm on Nov 23, 2005 (gmt 0)

10+ Year Member



Hi all,

I'm trying to figure out how to remove duplicate entries from a VB array. Can anyone point me in the right direction?

Thanks!

carguy84

11:27 pm on Nov 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can think of one way, but its speed is debatable.

Create a second array, and step through the first array inserting the value into the second array. Before each insert tho, loop thru the second array checking to see if that value exists.

Chip

aspdaddy

9:32 am on Nov 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why not use a Scripting.Dictionary instead, its more efficient than an array and doesnt allow duplicate keys.

erikcw

3:23 pm on Nov 24, 2005 (gmt 0)

10+ Year Member



Firstly - Happy Turkey Day!

So with the Dictionary.Script I would load all my data as the keys, and a random value.

This will ensure that there is no duplicate data and I can then somehow loop through and retrieve the data again?

But how do I loop through it if the key is non numeric? Seems like it would be difficult to get my data back out...

emsaw

6:00 pm on Nov 25, 2005 (gmt 0)

10+ Year Member



It's much simpler than you think.. I think :P


<%@ Language="VBScript" %>
<% OPTION EXPLICIT %>
<%
Function RemoveDupsAndOutput()
Dim myArray, myDict, index

Set myDict = CreateObject("Scripting.Dictionary")
myArray = Split("red,orange,yellow,green,blue,indigo,violet,green,green,green,green",",")

for index = 0 to UBound(myArray)
if ( not myDict.Exists(myArray(index)) ) then
myDict.Add myArray(index), myArray(index)
Response.Write myDict(myArray(index)) + "<br>"
end if
next
End Function
%>
<html>
<head>
<title>DupCheck</title></head>
<body>
<% RemoveDupsAndOutput() %>
</body>
</html>

So basically, you'd have your array, and insert into the dictionary, checking before each insert if the key already exists..

HTH,

Mark

aspdaddy

10:21 am on Nov 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



But how do I loop through it if the key is non numeric

Its a collection, so you use a collection-friendly loop :)

for each key in objDict
response.write objDict.Item(key)
next

I dont think you need to worry about checking if the key exists each time you insert, just insert them and let the container work for you, preventing duplicates etc.

emsaw

8:12 pm on Nov 28, 2005 (gmt 0)

10+ Year Member



I believe erikcw said he had an array, so I suggested looping through the array by index. AFAIK, if you try to insert the same key twice, it will error out.. unless you set ON ERROR RESUME NEXT, which will mask other problems, no? So I believe the key check is necessary.

-Mark

aspdaddy

9:43 am on Nov 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are right, it does give an error if you try and insert a duplicate. But you could just handle error 457 in your sub