Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2652
  • Last Modified:

finding duplicates in arraylist

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
aareedy
Asked:
aareedy
1 Solution
 
Jaime OlivaresCommented:
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
 
Göran AnderssonCommented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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
 
aareedyAuthor Commented:
Thanks! Great help!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now