Results 1 to 1 of 1

Thread: Eliminate Duplicates In An Array

Threaded View

  1. #1

    Thread Starter
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    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:
    1. Option Explicit
    2.  
    3. Dim A(9) As Long
    4. Dim B() As Long
    5. Dim I As Long, J As Long, K As Long
    6.  
    7. Private Sub Remove_Duplicates(Old_Array() As Long, New_Array() As Long)
    8.  
    9.     Dim I As Long, J As Long, K As Long
    10.  
    11.     ReDim New_Array(0) As Long
    12.     Dim Dup As Boolean
    13.     For I = 0 To UBound(Old_Array)
    14.         Dup = False
    15.         For J = 0 To UBound(New_Array)
    16.             If Old_Array(I) = New_Array(J) And I <> 0 Then
    17.               Dup = True
    18.               Exit For
    19.             End If
    20.         Next J
    21.         If Dup = False Then
    22.             ReDim Preserve New_Array(K) As Long
    23.             New_Array(K) = Old_Array(I)
    24.             K = K + 1
    25.         End If
    26.     Next I
    27.  
    28. End Sub
    29.  
    30. Private Sub Form_Activate()
    31.  
    32.     A(0) = 0
    33.     A(1) = 1
    34.     A(2) = 2
    35.     A(3) = 3
    36.     A(4) = 4
    37.     A(5) = 5
    38.     A(6) = 6
    39.     A(7) = 7
    40.     A(8) = 8
    41.     A(9) = 9
    42.  
    43.     Remove_Duplicates A(), B()
    44.    
    45.     For I = 0 To UBound(B)
    46.         Print B(I)
    47.     Next I
    48.  
    49. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width