comparing items in an array...

Hey guys… Can anyone help me with this please ?

I have a Myarray(3) and I need to check it for duplicates.

So, I must compare 0 to 1, 2, 3.
Then, 1 to 0, 2, 3,
then, 2 to 0, 1, 3,
and at last, 3 to 0, 1, 2.

Right ?

What’s the right/best way to do it ?

You only need an algorithm if this will ever be applied to a larger array.
What you typed is good enough to do it for a 4 element array

if M(0) = M(1) or M(0) = M(2) or M(0) = M(3) then…

if M(1) = M(2) or M(1) = M(3) then…

if M(2) = M(3) then…

You dont need to check that M(3) = M(1) as you did that on the first row.

For bigger numbers of array elements, you need nested loops

For x = 0 to 2
for y = 1 to 3
if M(x) = M(y)

Homework, I guess.

Thanks. Jeff

Just one more thing…

If I’d use the nested loops approach in the case of a larger array… Would I not get a false positive when (for example) x=1 and y=1. Comparing the same Id"s would get me duplicated items… Right?

For x = 0 to 2
for y = 1 to 3
if M(x) = M(y)

for x=0 to m.ubound-1
for y=x+1 to m.ubound

It doesn’t matter for small datasets but for larger unsorted datasets, I prefer using a dictionary.

To remove duplicates:

[code] Dim d As New Dictionary
For Each s As String In myArray
d.Value(s) = True
Next s

Dim uniqueArray() As String
For Each v As Variant In d.keys
uniqueArray.Append(v)
Next v[/code]

…or just count the duplicates:

Dim dupCnt As Integer Dim d As New Dictionary For Each s As String In myArray If d.HasKey(s) Then dupCnt = dupCnt + 1 d.value(s) = True Next s

use of a dictionary might not suit the purpose, as you then lose control over the original physcial location, which may or may not be important