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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

eliminate duplicate value in array in ASP

Status
Not open for further replies.

Sot

Programmer
Oct 10, 2002
24
US
I have 4 diffrent array a(6),B(6),C(6) and d(6)

a(6) contains 120,120,145,326,145,120
b(6) contains 333,120,145,336,145,120
c(6) contains 120,444,145,326,145,444
d(6) contains 333,120,145,326,145,120

I want to eleminate the duplicate value each array and append in an single new array..
Note: Array values may be less then 6...
Regards,
Sot

 
You mean you want to take 4 arrays, with any number of values, and put them all into one array wit no repeating values?

The brute force method for this would be:
Code:
'create a new array
Dim endArray, cnt
'count maximum number of elements we might have
cnt = UBound(a) + 1 + UBound(b) + 1 + UBound(c) + 1 + UBound(d)
'redimension the array to hold that max number
ReDim endArray(cnt)

'define a function that accepts a number, checks if it is in the endArray, and adds it if it isn't (returns true) or throws it away if it's already in there (returns false)
Function AddToArray(aValue)
   Dim i
   'assume we will be adding it
   AddToArray = True
   'loop through the array
   For i = 0 to UBound(endArray)
      'check if the current value is equal to the passed value, if so we won't be adding the passed value
      If endArray(i) = aValue Then AddToArray = False
      'check if we have hit an empty array cell
      If endArray(i) = "" Then Exit For
   Next

   'Now that we have exited, if AddToArray is still True, set the value to the last value of i
   If AddToArray = True Then endArray(i) = aValue

   'The value of AddToArray will automatically be passed back
End Function

'Loop through all four arrays, attempting to add the values. Count how many we actually add.
cnt = 0

Dim j
For j = 0 to UBound(a)
   If AddToArray(a(j)) = True Then cnt = cnt + 1
Next

For j = 0 to UBound(b)
   If AddToArray(b(j)) = True Then cnt = cnt + 1
Next

For j = 0 to UBound(c)
   If AddToArray(c(j)) = True Then cnt = cnt + 1
Next

For j = 0 to UBound(d)
   If AddToArray(d(j)) = True Then cnt = cnt + 1
Next

'cnt now holds the number of unique items we have
'so lets redim endArray by cnt-1
ReDim Preserve endArray(cnt-1)

This was written on the fly more to show the principle than to be workable. I'm rather tired so it may have some errors in it.

Another method would be to create a link list node object. Basically it would have the following functionality:
1) Add(aValue) - the node would check it's own value, if aValue is differant it would then check if it has a child node
- if the child node is null, it would create a child niode and give it that value
- if the child node is not null it would simply call it's childs Add(aValue) function
2) Count - if child was null it would return 1, if child is not null it would return 1 + child.Count
3) toArray(arr) - if the array wasn't initialized then the node (first one) would call it's own count function, redim the array
each node would set it's value: arr(UBound(arr)-me.Count) = myValue
it would then return Child.toArray(arr) so that each child could add their value

Anyways, to tired to write that one up, not sure if it would be fasterm, but it would ne niftier :)

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
minilogo.gif alt=tiernok.com
The never-completed website
 
Here it is a simple solution.
Let the Dictionary object to deal with the duplicate items.
We add items using as a key name the values you want to be just 1 time. The object deals with duplicate key names.

Code:
on error resume next
set sol=CreateObject("Scripting.Dictionary")
for i=1 to 6 
 sol.Add a(i),1
 sol.Add b(i),1
 sol.Add c(i),1
 sol.Add d(i),1
next
val=""
for each a in sol.Keys
	val=val&a&" "
next

You should have all uniques values in val string

________
George, M
 
George,
I tried ur example....
I am getting the following error
Error Type:
Microsoft VBScript runtime (0x800A01C9)
This key is already associated with an element of this collection
Ragards,
Sot
 
Did you used this line?
on error resume next
It shouldnt have the error

________
George, M
 
Dear George,
Inputs:
dx1=195.4,195.4,195.4,441.9,569.5,301.3
dx2=301.3,280.0,280.0,842.13,980.9


on error resume next
Dim sol,val
set sol=CreateObject("Scripting.Dictionary")
for i=1 to 6
'sol.Add a(i),1
sol.Add dx1(i),1
sol.Add dx2(i),1
'sol.Add c(i),1
'sol.Add d(i),1
next
val=""
for each dx1 in sol.Keys
val=val&dx1&","

next

Response.Write val

Output is val=195.4,301.3,280.0,441.9,842.13,569.5,980.9,,



But expexted is output=195.4,441.9,569.5,301.3,280.0,842.13,980.9


Regards,
Sot
 
Dear George,
Thanks for ur great time....Really you helped me lot.....Great solution...
 
Well it seems that it wont sort the Keys list, so you place the Keys into an array and then sort it out.

________
George, M
 
Ok i find out how it is done. You have to use it this way

for i=1 to 6
sol.Add dx1(i),1
next

for i=1 to 5
sol.Add dx2(i),1
next

so it will add in the right sequence



________
George, M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top