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  
 
 [ 3 post ] 

 Relevant Pages 

1. Eliminating duplicate memo fields must be done !!!

2. Eliminating duplicate records with abbreviated addresses

3. Eliminating Duplicates from Report

4. Eliminating Duplicate Values from Combo Box

5. SQL question to eliminate duplicate names

6. eliminating duplicates in a database table

7. Array with duplicate records

8. Adding duplicate values in VB Array

9. Replacing Duplicates in an Array

10. Parse Duplicates from a String Array

11. Duplicate array values

12. Removing Duplicate Array Entries

 

 
Powered by phpBB® Forum Software