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.
Printable View
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.
Not extremely knowledgable on arrays, but couldn't you look for the item in the array before adding the next item to the array?
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).
You can loop through them.
And you would use it like: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
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
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.
The last post was for James.
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.
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
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