Duplicates in Arraylist

  • Thread starter Thread starter Guest
  • Start date Start date
How do I remove duplicates from an arraylist?

If it's no problem to sort the list first, you can do it with a single loop
(code is in VB.NET):

arr.Sort()
Dim count As Integer = arr.Count
Dim i As Integer
For i = count - 1 To 1 Step -1
If (arr(i).ToString() = arr(i - 1).ToString()) Then
arr.RemoveAt(i)
End If
Next i
 
Hi,

Just looping, testing it to the same arraylist starting the test index after
the one you are testing and when found an equality move all items up one
item from starting one after the found one and remove the last item. And
proceed to the next one only when there has been no equality.

Very simple in my opinion.

I hope this helps?

Cor
 
Hi,

Seeing the solution from Ed, moving up is of course crazy in my text, goes
automaticly when you removeat.

And in my text I tried to avoid the sort so that is up to you

Cor
 
How do I remove duplicates from an arraylist?

Very quick and dirty (the first occurance of each item will remain in the
list):

\\\
Dim al As New ArrayList()
al.AddRange(New String() {"Bla", "Foo", "Foo", "Goo", "Bla", "Baz"})
Dim ht As New Hashtable()
Dim ItemsToRemove As New ArrayList()
Dim Item As String
Dim i As Integer
For i = 0 To al.Count - 1
Item = al(i)
If ht.Contains(Item) Then
ItemsToRemove.Add(i)
Else
ht.Add(Item, Nothing)
End If
Next i
For i = ItemsToRemove.Count - 1 To 0 Step -1
al.RemoveAt(ItemsToRemove(i))
Next i
For i = 0 To al.Count - 1
MsgBox(al(i))
Next i
///
 
Hi,

After that I saw all in code I could not resist to do the same

Here my contribution.

\\\
Dim al As New ArrayList
al.AddRange(New String() {"Bla", "Foo", "Foo", "Goo", "Bla", "Baz", "Foo"})
Dim pos As Integer = 0
Do Until pos = al.Count
Dim i As Integer
For i = al.Count - 1 To pos + 1 Step -1
If al(pos).ToString = al(i).ToString Then
al.RemoveAt(i)
End If
Next
pos += 1
Loop
///
 
Herfried,

This was as well my first thought, I found it a little bit overdone however.

Cor
 
Cor Ligthert said:
This was as well my first thought, I found it a little bit overdone
however.

What's overdone with the solution, if it solves the problem and doesn't have
a bad runtime performance?! Other than the solutions posted in this thread
my solution doesn't change the order of the items stored in the arraylist.
 
What's overdone with the solution, if it solves the problem and doesn't
have a bad runtime performance?! Other than the solutions posted in this
thread my solution doesn't change the order of the items stored in the
arraylist.
Wrong my solution does not change the order either, however it is not a
question of better or worse, only that I thought about it, and than found it
to much rows.

I had another solution in mind by the way (which did as well not change the
sequence) however I think that the last one I sent will perform very fast.

The advantage of yours is that it creates automaticly an extra array and
there are situations where that maybe is needed.

Cor
 
Cor Ligthert said:
Wrong my solution does not change the order either, however it is not a
question of better or worse, only that I thought about it, and than found
it to much rows.

Sorry, I was speaking at the time of posting my solution... :-(.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

ArrayList 1
Finding 1 object index in an arraylist 5
ArrayList memory problem 3
Arraylist 3
newbie: ArrayList memory leak? 3
Resizing an arraylist 5
Creating ArrayList to store data 3
DataGrid and ArrayList 1

Back
Top