|
-
Aug 24th, 2000, 03:35 PM
#1
Thread Starter
Lively Member
Array(0) = "A"
Array(1) = "B"
Array(2) = "C"
Array(3) = "D"
Array(4) = "A"
What is the fastest and shortest codes to find the duplicates in an array?
Unrelated, how do you put the thumb icon in the body of my posting. I know how to put faces.
-
Aug 24th, 2000, 03:45 PM
#2
PowerPoster
Not extremely knowledgable on arrays, but couldn't you look for the item in the array before adding the next item to the array?
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
Aug 24th, 2000, 03:47 PM
#3
I don't know the answer to your first question, but there have been a number of posts asking how to remove duplicates so if you do a search you may find the answer. But if you can use a collection instead of an array, then you can check for a duplicate key with only 2 or 3 lines of code and I'm sure that the collection approach is faster than any approach using an array (unless there is some API that you can use).
-
Aug 24th, 2000, 03:49 PM
#4
You can loop through them.
Code:
Function CheckArray(vValue As Variant, vArray) As Boolean
For I = LBound(vArray) To UBound(vArray)
If vArray(I) = vValue Then CheckArray = True
Next I
End Function
And you would use it like:
Code:
Dim MyArray(50) As Byte
MyArray(1) = 3
If CheckArray(3, MyArray()) = True Then
'Already in array
Print "Already in array"
Else
'Not in array
Print "Not in Array"
End If
-
Aug 24th, 2000, 03:51 PM
#5
Thread Starter
Lively Member
No! I can't do it like that for other reasons.
Right now, I am using a For or Do loop and have it loop through.
I don't want to do it like that because if my array goes up to 100, that means it has to do 10,000 comparison.
-
Aug 24th, 2000, 03:55 PM
#6
Thread Starter
Lively Member
The last post was for James.
-
Aug 24th, 2000, 04:06 PM
#7
Thread Starter
Lively Member
Megatron, the loop that you made only checks for a given value. I am looking for something that gives the entire array and it will returns only the duplicate ones. Thank you Megatron.
Martin, what is the difference between an array and a collection. I thought it was use in a similar method.
-
Aug 24th, 2000, 04:35 PM
#8
_______
<?>
Code:
'this is very Mickey but it works
'someone can fix it to compare 1st and last
Sub MyCompare(iArray As Variant)
Dim myString
Dim LoopOne As Long
Dim LoopTwo As Long
Dim x As Integer, y As Integer
x = LBound(iArray)
y = UBound(iArray)
'for some reason I'm not checking the 1st against the last
'so I improvise and use it this way
If iArray(x) = iArray(y) Then
dup = True
myString = iArray(x)
End If
'check the array
For LoopOne = UBound(iArray) To LBound(iArray) Step -1
For LoopTwo = LBound(iArray) + 1 To LoopOne
If iArray(LoopTwo - 1) = iArray(LoopTwo) Then
myString = iArray(LoopTwo)
dup = True
End If
Next LoopTwo
Next LoopOne
If dup = True Then MsgBox "Duplicate " & myString
End Sub
Private Sub Command1_Click()
Dim myArray(4) As Variant
myArray(0) = "A"
myArray(1) = "A"
myArray(2) = "C"
myArray(3) = "A"
myArray(4) = "W"
Call MyCompare(myArray)
End Sub
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Aug 24th, 2000, 07:23 PM
#9
Fanatic Member
If you're putting a loop in a loop then you are (as was said above) looping Ubound(Array) ^ 2 times.
It would be quicker to sort the array using a quicksort (or a bubble sort if the array was usually mostly sorted, or a counting sort if you were only dealing with numbers) then loop though the array once checking if Array(x) = Array(x+1)
I'm sure that if you do a search on this site for Quicksort you'll get several examples of the algorithm.
That'd be quicker than using a collection I think
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
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
|