Results 1 to 11 of 11

Thread: [RESOLVED] Eliminate Duplicates In Array

  1. #1

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

    Resolved [RESOLVED] Eliminate Duplicates In Array

    I'm trying to eliminate duplicates from one array, and store them into another, but this code doesn't seem to do it right. Can anyone give me a hand on fixing it. Thanks in advance.

    [EDIT] Fixed subscript out of range error.

    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 Form_Activate()
    8.  
    9.     A(0) = 0
    10.     A(1) = 1
    11.     A(2) = 2
    12.     A(3) = 3
    13.     A(4) = 4
    14.     A(5) = 5
    15.     A(6) = 6
    16.     A(7) = 7
    17.     A(8) = 8
    18.     A(9) = 9
    19.    
    20.     ReDim B(0) As Long
    21.  
    22.     For I = 0 To UBound(A)
    23.         For J = 0 To UBound(B)
    24.             If A(I) <> B(J) Then
    25.                 If J = UBound(B) Then
    26.                     B(J) = A(I)
    27.                     K = K + 1
    28.                     ReDim Preserve B(K) As Long
    29.                 End If
    30.             ElseIf A(I) = B(J) Then
    31.                 Exit For
    32.             End If
    33.         Next J
    34.     Next I
    35.    
    36.     For I = 0 To UBound(B)
    37.         Print B(I)
    38.     Next I
    39.  
    40. End Sub

  2. #2
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Eliminate Duplicates In Array

    I dont believe that your code it getting inside the 2nd for loop, because your going from 0 to Ubound(B) ( Ubound(B) = 0 ).

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  3. #3

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

    Re: Eliminate Duplicates In Array

    Ok I got it, but the only way it can work right is if the array B(0) has a value that is not 0. So I made it -1 but that's not good. Any other solutions? Here's the more improved code:

    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 Form_Activate()
    8.  
    9.     A(0) = 0
    10.     A(1) = 0
    11.     A(2) = 2
    12.     A(3) = 3
    13.     A(4) = 4
    14.     A(5) = 5
    15.     A(6) = 6
    16.     A(7) = 7
    17.     A(8) = 8
    18.     A(9) = 9
    19.    
    20.     ReDim B(0) As Long
    21.  
    22.     [b]B(0) = -2147483648#[/b]
    23.  
    24.     For I = 0 To UBound(A)
    25.         For J = 0 To UBound(B)
    26.             If A(I) <> B(J) Then
    27.                 If J = UBound(B) Then
    28.                     [b]ReDim Preserve B(K) As Long
    29.                     B(K) = A(I)          'I replaced J with K
    30.                     K = K + 1[/b]
    31.                 End If
    32.             ElseIf A(I) = B(J) Then
    33.                 Exit For
    34.             End If
    35.         Next J
    36.     Next I
    37.    
    38.     For I = 0 To UBound(B)
    39.         Print B(I)
    40.     Next I
    41.  
    42. End Sub

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Eliminate Duplicates In Array

    This seems to do the trick:
    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 Form_Activate()
    8.  
    9.     A(0) = 0
    10.     A(1) = 1
    11.     A(2) = 2
    12.     A(3) = 3
    13.     A(4) = 4
    14.     A(5) = 5
    15.     A(6) = 6
    16.     A(7) = 7
    17.     A(8) = 8
    18.     A(9) = 9
    19.    
    20.     ReDim B(0) As Long
    21.     Dim dup As Boolean
    22.     For I = 0 To UBound(A)
    23.         dup = False
    24.         For J = 0 To UBound(B)
    25.             If A(I) = B(J) And I <> 0 Then
    26.               dup = True
    27.               Exit For
    28.             End If
    29.         Next J
    30.         If dup = False Then
    31.             B(J - 1) = A(I)
    32.             K = K + 1
    33.             ReDim Preserve B(K) As Long
    34.         End If
    35.     Next I
    36.    
    37.     For I = 0 To UBound(B) - 1
    38.         Print B(I)
    39.     Next I
    40. End Sub

    K seemed to have one extra, which was 0, so I don't show it, and I checked that i<>0 (for the first time thru)

  5. #5

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

    Re: Eliminate Duplicates In Array

    Quote Originally Posted by Jumpercables
    I dont believe that your code it getting inside the 2nd for loop, because your going from 0 to Ubound(B) ( Ubound(B) = 0 ).
    It has to. And updates the second array, making it have more elements for how ever many from the first array is not a duplicate.


    Oh and dglienna, I found a better way, but I don't like the idea of making the first element of B() a non zero value.

  6. #6

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

    Re: Eliminate Duplicates In Array

    Oh nevermind, I'll just make B(0) = -2147483648#, since odds are, it'll never be that number. I updated the above code.

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: [RESOLVED] Eliminate Duplicates In Array

    Did you try mine? It works fine with all conditions. I would sort the info first, though. Use a bubble sort. I thought my version was good, though. That's why I posted it for you.

  8. #8

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

    Re: [RESOLVED] Eliminate Duplicates In Array

    Yours had an extra element in the array. Mine didn't. But it was nice to see a variation of it.

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: [RESOLVED] Eliminate Duplicates In Array

    If you know it's going to be there, it's easy enough to redim out. I was following your logic of adding the elements. Plus, I think mine would be faster.

  10. #10

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

    Re: [RESOLVED] Eliminate Duplicates In Array

    Hehe, I fixed your version of it, with the help of my solution:

    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 Form_Activate()
    8.  
    9.     A(0) = 0
    10.     A(1) = 1
    11.     A(2) = 2
    12.     A(3) = 3
    13.     A(4) = 4
    14.     A(5) = 5
    15.     A(6) = 6
    16.     A(7) = 7
    17.     A(8) = 8
    18.     A(9) = 9
    19.    
    20.     ReDim B(0) As Long
    21.     Dim dup As Boolean
    22.     For I = 0 To UBound(A)
    23.         dup = False
    24.         For J = 0 To UBound(B)
    25.             If A(I) = B(J) And I <> 0 Then
    26.               dup = True
    27.               Exit For
    28.             End If
    29.         Next J
    30.         If dup = False Then
    31.            [b]ReDim Preserve B(K) As Long
    32.             B(K) = A(I)    'I replaced J with K
    33.             K = K + 1[/b]
    34.         End If
    35.     Next I
    36.    
    37.     For I = 0 To UBound(B)
    38.         Print B(I)
    39.     Next I
    40. End Sub

  11. #11

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

    Re: [RESOLVED] Eliminate Duplicates In Array

    I even created a Sub out of it to make it easier

    VB Code:
    1. Private Sub Remove_Duplicates(Old_Array() As Long, New_Array() As Long)
    2.  
    3.     Dim I As Long, J As Long, K As Long
    4.  
    5.     ReDim New_Array(0) As Long
    6.     Dim Dup As Boolean
    7.     For I = 0 To UBound(Old_Array)
    8.         Dup = False
    9.         For J = 0 To UBound(New_Array)
    10.             If Old_Array(I) = New_Array(J) And I <> 0 Then
    11.               Dup = True
    12.               Exit For
    13.             End If
    14.         Next J
    15.         If Dup = False Then
    16.             ReDim Preserve New_Array(K) As Long
    17.             New_Array(K) = Old_Array(I)
    18.             K = K + 1
    19.         End If
    20.     Next I
    21.  
    22. 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