Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove Duplicates from Array

Status
Not open for further replies.

firsttube

Technical User
Apr 21, 2004
165
CA
I have an array like this:
Andy,Dave,Steve,Andy

And I want to change it to be this:
Andy,Dave,Steve

So I need to remove the duplicate from the array. How might this be done?



Information is not Knowledge, Knowledge is not Wisdom, Wisdom is not Truth, Truth is not Beauty, Beauty is not Love, Love is not Music, Music is the best.
 
create a new array and do this(javascript):

for (i = 0; i < oldarray.length; i++)
{

newarray[oldarray] = oldarray;

}

-DNG
 
if it was sorted you wouldnt need a loop within a loop.
 
I think the closest method in VBScript to DNG's javascript method would be to use the Dictionary object. This is the closest thing I know of to a keyed collection in VBScript (not counting custom classes, I have written a few myself). Basically something like:
Code:
<%
Option Explicit

'an initial sample array tp play with
Dim myArray
myArray = Array("fish","cat","dog","fish","bob","blue","green","red","yeller","fish","dog","weasel","snake","george")


'unique items with Dictionary object
Dim myDict, elem
Set myDict = Server.CreateObject("Scripting.Dictionary")
For Each elem In myArray
	If Not myDict.Exists(elem) Then myDict.Add elem, elem
Next

'Sample output
Response.Write "<table><tr><th>Dictionary Values</th></tr>"
For Each elem in myDict.Items
	Response.Write "<tr><td>" & elem & "</td></tr>"
Next
Response.Write "</table>"
%>

Sorting the data would also give you an opportunity to make it unique, however I am not aware of any built-in methods for this and unless you need the data sorted I would think it would be wasteful of system resources to do it. On the other hand if you really wanted to do it I have a few sort methods I impleneted in VBScript at home as well as a SortedCollection object I created that allows keyed collecitons, uniqueness, and built-in sorting.
In fact if you search the forums you may even be able to find some of these collections or sorting methods as I or someone else has probably posted them at some point or another. In fact I swear I rememer a couple posts with sorting methods and I know for a fact I have posted a few custom collection objects that could be easily converted to offer alphabetical sorting (maybe search for "bookcollection"?)

-T

barcode_1.gif
 
Or if the array is originally built from a database query you could maybe just get the database to do the sorting with an ORDER BY clause ... or perhaps use the ADO recordset object to do the sorting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top