|
-
Dec 19th, 2005, 09:05 PM
#1
[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:
Option Explicit
Dim A(9) As Long
Dim B() As Long
Dim I As Long, J As Long, K As Long
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
ReDim B(0) As Long
For I = 0 To UBound(A)
For J = 0 To UBound(B)
If A(I) <> B(J) Then
If J = UBound(B) Then
B(J) = A(I)
K = K + 1
ReDim Preserve B(K) As Long
End If
ElseIf A(I) = B(J) Then
Exit For
End If
Next J
Next I
For I = 0 To UBound(B)
Print B(I)
Next I
End Sub
Last edited by Jacob Roman; Dec 19th, 2005 at 09:20 PM.
-
Dec 19th, 2005, 10:00 PM
#2
Fanatic Member
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 ).
-
Dec 19th, 2005, 10:00 PM
#3
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:
Option Explicit
Dim A(9) As Long
Dim B() As Long
Dim I As Long, J As Long, K As Long
Private Sub Form_Activate()
A(0) = 0
A(1) = 0
A(2) = 2
A(3) = 3
A(4) = 4
A(5) = 5
A(6) = 6
A(7) = 7
A(8) = 8
A(9) = 9
ReDim B(0) As Long
[b]B(0) = -2147483648#[/b]
For I = 0 To UBound(A)
For J = 0 To UBound(B)
If A(I) <> B(J) Then
If J = UBound(B) Then
[b]ReDim Preserve B(K) As Long
B(K) = A(I) 'I replaced J with K
K = K + 1[/b]
End If
ElseIf A(I) = B(J) Then
Exit For
End If
Next J
Next I
For I = 0 To UBound(B)
Print B(I)
Next I
End Sub
Last edited by Jacob Roman; Dec 19th, 2005 at 10:20 PM.
-
Dec 19th, 2005, 10:01 PM
#4
Re: Eliminate Duplicates In Array
This seems to do the trick:
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 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
ReDim B(0) As Long
Dim dup As Boolean
For I = 0 To UBound(A)
dup = False
For J = 0 To UBound(B)
If A(I) = B(J) And I <> 0 Then
dup = True
Exit For
End If
Next J
If dup = False Then
B(J - 1) = A(I)
K = K + 1
ReDim Preserve B(K) As Long
End If
Next I
For I = 0 To UBound(B) - 1
Print B(I)
Next I
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)
-
Dec 19th, 2005, 10:04 PM
#5
Re: Eliminate Duplicates In Array
 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.
-
Dec 19th, 2005, 10:18 PM
#6
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.
-
Dec 19th, 2005, 10:25 PM
#7
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.
-
Dec 19th, 2005, 10:28 PM
#8
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.
-
Dec 19th, 2005, 10:31 PM
#9
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.
-
Dec 19th, 2005, 10:35 PM
#10
Re: [RESOLVED] Eliminate Duplicates In Array
Hehe, I fixed your version of it, with the help of my solution:
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 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
ReDim B(0) As Long
Dim dup As Boolean
For I = 0 To UBound(A)
dup = False
For J = 0 To UBound(B)
If A(I) = B(J) And I <> 0 Then
dup = True
Exit For
End If
Next J
If dup = False Then
[b]ReDim Preserve B(K) As Long
B(K) = A(I) 'I replaced J with K
K = K + 1[/b]
End If
Next I
For I = 0 To UBound(B)
Print B(I)
Next I
End Sub
-
Dec 19th, 2005, 10:48 PM
#11
Re: [RESOLVED] Eliminate Duplicates In Array
I even created a Sub out of it to make it easier 
VB Code:
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|