Selecting unique elements of an array... 
Author Message
 Selecting unique elements of an array...

Hello,

I have a array consisting of a finite number of different
strings but each string can occur a random number of times
(in different array elements). I want to get only the
unique values.  For example:  With an array consisting
of "860","860","861","884","884","884", I want to
return "860,861,884".

I'm about to write something that traverses the array and
returns only the unique strings.  Before doing so, I
checked the VBScript reference material to see if any
built in functions exist for this purpose.  Couldn't find
any but may have misse it.  

Does anyone know if such a built in function exists.

Thanks in advance,
JR



Mon, 21 Nov 2005 02:41:28 GMT  
 Selecting unique elements of an array...
This thread may help you..
http://beta.experts-exchange.com/Programming/Programming_Languages/Vi...

--
Veign
(Would like to be an MVP (Visual Basic))
www.veign.com
Application Design Section
http://www.veign.com/information/application/info_app.html
------

Quote:
> Hello,

> I have a array consisting of a finite number of different
> strings but each string can occur a random number of times
> (in different array elements). I want to get only the
> unique values.  For example:  With an array consisting
> of "860","860","861","884","884","884", I want to
> return "860,861,884".

> I'm about to write something that traverses the array and
> returns only the unique strings.  Before doing so, I
> checked the VBScript reference material to see if any
> built in functions exist for this purpose.  Couldn't find
> any but may have misse it.

> Does anyone know if such a built in function exists.

> Thanks in advance,
> JR



Mon, 21 Nov 2005 02:49:32 GMT  
 Selecting unique elements of an array...
You can use some On Error'ing and dictionary object and the fact that you
can't have duplicate keys in a dictionary object to do this.

<%

arrYourNumbers = Array(3,4,5,4,6)

Response.Write "Before filtering: <BR>"
For intCounter = LBound(arrYourNumbers) To UBound(arrYourNumbers)
 Response.Write arrYourNumbers(intCounter) & "<BR>"
Next

Response.Write "<HR>"

Set objDict = Server.CreateObject("Scripting.Dictionary")
 On Error Resume Next
    For intCounter = LBound(arrYourNumbers) To UBound(arrYourNumbers)
        objDict.Add arrYourNumbers(intCounter), arrYourNumbers(intCounter)
    Next

Redim arrYourNumbers(objDict.Count) '''Note:  no PRESERVE statement
    arrYourNumbers = objDict.Items

Set objDict = Nothing

Response.Write "After filtering: <BR>"
For intCounter = LBound(arrYourNumbers) To UBound(arrYourNumbers)
    Response.Write arrYourNumbers(intCounter) & "<BR>"
Next

%>

Ray at work


Quote:
> Hello,

> I have a array consisting of a finite number of different
> strings but each string can occur a random number of times
> (in different array elements). I want to get only the
> unique values.  For example:  With an array consisting
> of "860","860","861","884","884","884", I want to
> return "860,861,884".

> I'm about to write something that traverses the array and
> returns only the unique strings.  Before doing so, I
> checked the VBScript reference material to see if any
> built in functions exist for this purpose.  Couldn't find
> any but may have misse it.

> Does anyone know if such a built in function exists.

> Thanks in advance,
> JR



Mon, 21 Nov 2005 02:50:52 GMT  
 Selecting unique elements of an array...
If your array is quite large (say many thousands),
then doing a (quadratic time) search through the array
for each element will become inefficient.

An ostensibly faster solution is to stuff each element into a dictionary:

Set myDct = CreateObject("Scripting.Dictionary")
strResult = aRay(lbound(aRay))
myDct.Add strResult, 1
For i=lbound(aRay)+1 to ubound(aRay)
    iF not myDct.exists(aRay(i)) then
        myDct.Add aRay(i), 1
    'Else              'Look ma!  a frequency counter
    '    myDct.item(aRay(i)) = myDct.item(aRay(i)) + 1
   End If
   strResult = strResult & "," & aRay(i)       'string form
next i
aComposite = myDct.Keys                           'array form
set myDct = Nothing

Enjoy,
Csaba Gabor from New York

Quote:

> Hello,

> I have a array consisting of a finite number of different
> strings but each string can occur a random number of times
> (in different array elements). I want to get only the
> unique values.  For example:  With an array consisting
> of "860","860","861","884","884","884", I want to
> return "860,861,884".

> I'm about to write something that traverses the array and
> returns only the unique strings.  Before doing so, I
> checked the VBScript reference material to see if any
> built in functions exist for this purpose.  Couldn't find
> any but may have misse it.

> Does anyone know if such a built in function exists.

> Thanks in advance,
> JR



Mon, 21 Nov 2005 03:14:29 GMT  
 Selecting unique elements of an array...
Hi,

If your values are empty, strings (without a CR or other delimiter of your
choice) or numbers, and you're willing to use a variant array, it only takes
5 lines (air code, watch for errors):

xArray2= vbCr
for i= 0 to ubound(xArray1)
if (instr(xArray2, vbCr & trim(xArray1(i)) & vbCr)=0) _
   then xArray2= xArray2 & trim(xArray1(i)) & vbCr
next
xArray2= split(mid(xArray2, 2, len(xArray2) -2), vbCr)

Joe Earnest


Quote:
> Hello,

> I have a array consisting of a finite number of different
> strings but each string can occur a random number of times
> (in different array elements). I want to get only the
> unique values.  For example:  With an array consisting
> of "860","860","861","884","884","884", I want to
> return "860,861,884".

> I'm about to write something that traverses the array and
> returns only the unique strings.  Before doing so, I
> checked the VBScript reference material to see if any
> built in functions exist for this purpose.  Couldn't find
> any but may have misse it.

> Does anyone know if such a built in function exists.

> Thanks in advance,
> JR



Mon, 21 Nov 2005 03:15:12 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. ordered array of unique elements

2. Selecting Random Element from an Array

3. Selecting specific array index elements

4. Selecting random array element

5. Array.BinarySearch not working: it skips array elements

6. array of arrays add elements

7. How to simulate selecting an item in a select element

8. How to dynamically select option in select element?

9. Can Arrays be unique to Clients?

10. Array has x elements. Set array(x) = newval

11. How can I copy a control array element to another control array element?

12. Select Unique Record Identifier problem with Linking SQL server

 

 
Powered by phpBB® Forum Software