Solved

finding duplicates in arraylist

Posted on 2007-12-01
4
2,627 Views
Last Modified: 2011-10-03
I am using the method below to find the duplicates in an array list and remove them.  It works, but only finds the first duplicate.  So for instance if the list is "1", "1","1","2","2","3", the unique list is 1, 1, 2, 3.  Anyone know why that is?
   public void topTalkers()
        {

            int i, j, k;

            ArrayList arr = new ArrayList();
            string[] unTemp = new string[recordNum];
           
            for (int x = 0; x < recordNum - 1; x++)
            {
                unTemp[x]= srcIP[x];
            }
            for (k = 0; k < unTemp.Length-1; k++)
            {
                arr.Add(unTemp[k].ToString());
            }
            arr.Sort();
 

            for (i = 0; i < arr.Count - 1; i++)
            {
                for (j = i + 1; j < arr.Count; j++)
                {
                    if (arr[i].Equals(arr[j]))
                    {
                        MessageBox.Show("duplicate removed: " + arr[j]);
                        arr.RemoveAt(j);
                    }
                }
            }

            for (k = 0; k < arr.Count; k++)
            {
                MessageBox.Show(arr[k].ToString());

            }
        }

0
Comment
Question by:aareedy
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 total points
ID: 20389304
try with:
for (i = 0; i < arr.Count - 1; i++)
            {
                for (j = i + 1; j < arr.Count;)
                {
                    if (arr[i].Equals(arr[j]))
                    {
                        MessageBox.Show("duplicate removed: " + arr[j]);
                        arr.RemoveAt(j);
                    }
                    else
                        j+;
                }
            }

0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20389399
As the list is sorted, you can reduce the comparisons you do drastically. You don't have to compare every item to all the other items, you only have to compare the items that are next to each other:
for (int i = 1; i < arr.Count;) {

   if (arr[i-1].Equals(arr[i]) {

      MessageBox.Show("Duplicate removed: " + arr[i].ToString());

      arr.RemoveAt(i);

   } else {

      i++;

   }

}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20389407
>> Anyone know why that is?
The problem is being caused by you removing the dup, which shuffles everything after it down one but then you increment j so you are missing the next one.

jaime_olivares suggested fix should over-come this.

Example: -

 0   1   2   3   4   5

"1","1","1","2","2","3", 

i=0, j=1 

arr[i] = 1

arr[j] = 1

dup found and removed

j++
 

 0   1   2   3   4   5

"1","1","2","2","3", 

i=0, j=2

arr[i] = 1

arr[j] = 2
 

dup missed

Open in new window

0
 

Author Closing Comment

by:aareedy
ID: 31412134
Thanks! Great help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 79
Export Table to CSV - Access to CSV - using python 18 100
Memory Usage 2 50
Class object 2 27
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now