473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Duplicates in Arraylist

How do I remove duplicates from an arraylist?
Nov 21 '05 #1
10 34779
an*******@discussions.microsoft.com wrote:
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

--

Edge
Nov 21 '05 #2
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
Nov 21 '05 #3
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
Nov 21 '05 #4
Hi,

In addition to the other comments you can use arraylist.contains to
see if the element is already in the arraylist to prevent adding duplicates.

http://msdn.microsoft.com/library/de...tainstopic.asp

Ken
-------------------------
<an*******@discussions.microsoft.com> wrote in message
news:0c****************************@phx.gbl...
How do I remove duplicates from an arraylist?
Nov 21 '05 #5
<an*******@discussions.microsoft.com> schrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #6
Herfried,

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

Cor
Nov 21 '05 #7
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
///

<an*******@discussions.microsoft.com>
How do I remove duplicates from an arraylist?

Nov 21 '05 #8
"Cor Ligthert" <no************@planet.nl> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #9
>
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
Nov 21 '05 #10
"Cor Ligthert" <no************@planet.nl> schrieb:
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.


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

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Marlene | last post by:
Hi All I have the following scenario, where I have found all the duplicates in a table, based on an order number and a part number (item).I might have something like this: Order PODate Rec...
3
by: _eddie_ | last post by:
I'm building an array of strings on the fly from a database. What is the best method for eliminating duplicates? (I can do this before or after the strings are added to the array)
11
by: steve smith | last post by:
Hi I'm still having some problems getting my head round this language. A couple of things don't seem to work for me. First I am trying to obtan a count of the number of words in a sting, so am...
11
by: paradox | last post by:
Basically I have an ArrayList of strings; I need a fast way of searching through and comparing the elements of the ArrayList and the return an ArrayList of items that have 3 Duplicates. For...
2
by: Mamatha | last post by:
Hi I have an application with listview.When i click on one button the data will be displayed like this in the listview: colA colB colC ----- ----- ------...
3
by: AK | last post by:
Hi Our product uses MS-SQL Server 2000. One of our customer has 10 installations with each installation stroring data in its own database. Now the customer wants to consolidate these databases...
3
by: ryan.paquette | last post by:
In the table there are 2 fields in which I wish to limit (i.e. No Duplicates) Although I do not want to limit them to "No Duplicates" separately. I need them to be limited to "No Duplicates" as...
1
by: AllBeagle | last post by:
Here is what might seem to be an odd request. Does anyone know how I can find a duplicate in an arraylist and delete both the duplicate and the original, leaving me with only the items that didn't...
1
by: tskmjk55 | last post by:
Recently, I have a requirement to develop a vb.net application wherein the input excel sheet data which has an average of 5000 records should be checked for Internal duplicates (duplicates within the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.