Solved

Removing duplicate in an Arraylist C#

Posted on 2014-01-23
2
314 Views
Last Modified: 2014-02-11
Hi;

I need to remove duplicate in an Arraylist of C#. I don't want to change it to a list.

What to do?

Sample code?

Regards.
0
Comment
Question by:jazzIIIlove
2 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 250 total points
ID: 39803574
Can you linq it?


            ArrayList l = new ArrayList();

Var DistinctItems =             l.AsQueryable().OfType<int>().Distinct();
0
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 250 total points
ID: 39805528
If you really want to keep ArrayList, you may use the following code.
Not very efficient but if your lists are not that big it should work OK. You create a new list and assign it finally to the original one. It would be a bit tricky to remove list items from original list :).

        private void button9_Click(object sender, EventArgs e)
        {
            ArrayList al = new ArrayList( );
            al.AddRange(new int[]{1,3,9,4,9,5,7,9,0,2,6,2});

            ArrayList al_temp = new ArrayList();
            printArrayList(al);
            foreach (int i in al)
            {
                if(!al_temp.Contains(i))
                {
                    al_temp.Add(i);
                }
            }
            al = al_temp;
            printArrayList(al);


        }
        // prints into output window
        private void printArrayList(ArrayList al)
        {
            
            foreach(int i in al)
            {
                System.Diagnostics.Debug.Write(" " + i + " ");
            }
            System.Diagnostics.Debug.WriteLine("");
        }

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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