Solved

Removing duplicate in an Arraylist C#

Posted on 2014-01-23
2
327 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

815 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

12 Experts available now in Live!

Get 1:1 Help Now