Eliminate Duplicates In An Array
Eliminating duplicates in an array can come in handy for many situations. Like if you have a long list of names, with some of them coming up twice or more times, and you wanted to only list everyone who has a different name, this will do it. But in this example, I used the Long data type instead of String. Here is a simple way to eliminate duplicates in an array:
VB Code:
Option Explicit
Dim A(9) As Long
Dim B() As Long
Dim I As Long, J As Long, K As Long
Private Sub Remove_Duplicates(Old_Array() As Long, New_Array() As Long)
Dim I As Long, J As Long, K As Long
ReDim New_Array(0) As Long
Dim Dup As Boolean
For I = 0 To UBound(Old_Array)
Dup = False
For J = 0 To UBound(New_Array)
If Old_Array(I) = New_Array(J) And I <> 0 Then
Dup = True
Exit For
End If
Next J
If Dup = False Then
ReDim Preserve New_Array(K) As Long
New_Array(K) = Old_Array(I)
K = K + 1
End If
Next I
End Sub
Private Sub Form_Activate()
A(0) = 0
A(1) = 1
A(2) = 2
A(3) = 3
A(4) = 4
A(5) = 5
A(6) = 6
A(7) = 7
A(8) = 8
A(9) = 9
Remove_Duplicates A(), B()
For I = 0 To UBound(B)
Print B(I)
Next I
End Sub