eliminate duplicates in array?
Author Message
eliminate duplicates in array?

Does anyone know of an easy way to eliminate duplicate numbers in an array?
For example:
(1, 2, 2, 3, 4, 5)
I want to null out that 2nd number 2 element, re-sort "array.sort(myArray)"
and be left with:
(1, 2, 3, 4, 5)

Any suggestions?

Thu, 29 Jul 2004 22:58:35 GMT
eliminate duplicates in array?
Cynic,
You will need to write some code to do it. Something like:

Dim a As New ArrayList()
a.AddRange(New Integer() {1, 2, 5, 2, 3, 4, 5})
a.Sort()

Dim i As Integer  = 1

While i <= a.Count - 1
If CInt(a(i)) = CInt(a(i - 1)) Then
a.RemoveAt(i)
Else
i += 1
End If
End While

Granted you could generalize this using the IComparable interface...

Hope this helps
Jay

Quote:
> Does anyone know of an easy way to eliminate duplicate numbers in an
array?
> For example:
>  (1, 2, 2, 3, 4, 5)
> I want to null out that 2nd number 2 element, re-sort

"array.sort(myArray)"
Quote:
> and be left with:
>  (1, 2, 3, 4, 5)

> Any suggestions?

Fri, 30 Jul 2004 02:12:35 GMT
eliminate duplicates in array?
thanks!

Quote:
> Cynic,
> You will need to write some code to do it. Something like:

>     Dim a As New ArrayList()
>     a.AddRange(New Integer() {1, 2, 5, 2, 3, 4, 5})
>     a.Sort()

>     Dim i As Integer  = 1

>     While i <= a.Count - 1
>         If CInt(a(i)) = CInt(a(i - 1)) Then
>             a.RemoveAt(i)
>         Else
>             i += 1
>         End If
>     End While

> Granted you could generalize this using the IComparable interface...

> Hope this helps
> Jay

> > Does anyone know of an easy way to eliminate duplicate numbers in an
> array?
> > For example:
> >  (1, 2, 2, 3, 4, 5)
> > I want to null out that 2nd number 2 element, re-sort
> "array.sort(myArray)"
> > and be left with:
> >  (1, 2, 3, 4, 5)

> > Any suggestions?

Fri, 30 Jul 2004 03:00:13 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages

Powered by phpBB® Forum Software