Click to See Complete Forum and Search --> : removing array duplicates


tomjowitt
August 28th, 2001, 03:51 AM
how would i go about removing duplicates from an array without outputting it to a database and using SQL?

berta
August 28th, 2001, 03:58 AM
I think U must parse your array.
If U wnat to avoid the duplicate U can use Collection and add the Alphanumeric key for each elemenet

hi,brt

<center>
<HR width=80%>
<img src='http://web.tiscali.it/bertaplanet/images/bertaplanet.gif'>
</center>

Andrew_Fryer
August 28th, 2001, 04:23 AM
Hello,

The way I would do it would be to sort the array into alphabetical or numerical order.

Once you have done that then you then find duplicates by doing the following in VB



loop
get the current array item
loop until no duplicate item found
remove the duplicate item
end loop
increment the counter
end





Andrew

berta
August 28th, 2001, 04:40 AM
yes, but for remove item U must use 2 array.
something like that...

dim temp_Array(ubound(myarray))
dim i as long,J as long,Z as long
dim flgDuplicated as boolean
dim myItem

z=0
for i=1 to ubound(myarray)
myItem=myArray(i)
flgDuplicated=false
for J=1 to ubound(myarray)
if J<>i then
if myItem=myarray(i) then
flgDuplicated=true
exit for
end if
end if
next J
if not flgDuplicated then
z=z+1
temp_array(z)=myitem
end if
next i

redim myarray(z)

for i=1 to z
myarray(i)=temp_array(i)
next i

hi,brt

<center>
<HR width=80%>
<img src='http://web.tiscali.it/bertaplanet/images/bertaplanet.gif'>
</center>

Iouri
August 28th, 2001, 06:38 AM
' Filter out duplicate values in an array and compact
' the array by moving items to "fill the gaps".
' Returns the number of duplicate values
'
' it works with arrays of any type, except objects
'
' The array is not REDIMed, but you can do it easily using
' the following code:
' a() is a string array
' dups = FilterDuplicates(a())
' If dups Then
' ReDim Preserve a(LBound(a) To UBound(a) - dups) As String
' End If

Function FilterDuplicates(arr As Variant) As Long
Dim col As Collection, index As Long, dups As Long
Set col = New Collection

On Error Resume Next

For index = LBound(arr) To UBound(arr)
' build the key using the array element
' an error occurs if the key already exists
col.Add 0, CStr(arr(index))
If Err Then
' we've found a duplicate
dups = dups + 1
Err.Clear
End If
' if we've found one or more duplicates so far
' we need to move elements towards lower indices
If dups Then
arr(index - dups) = arr(index)
arr(index) = Empty
End If
Next

' return the number of duplicates
FilterDuplicates = dups

End Function



Iouri Boutchkine
iouri@hotsheet.com

Andrew_Fryer
August 28th, 2001, 04:11 PM
Hello,

The way I was considering would mean you could have a one-dimensional array and be able to delete items without having to resort to any form of indexing. For instance in the sorted list:

array1 : A
array2 : B
array3 : B
array4 : B
array5 : B
array6 : C
array7 : C
array8 : D
array9 : E

At the start the first loop would read the letter A, it would then enter the second loop and look for further instances of A. None would be found so the array would continue.

The next item found would be B. The second loop would find another instance of B, so the remainder of the items in the array would be moved down to remove this. The array would now be as follows:

A,B,B,B,C,C,D,E

The second loop would continue until no match was found whereby there would be only one instance of B. The first loop would then continue for the next letter and so on.

The final array would be A,B,C,D,E - with no duplicates.

Andrew