Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Removing duplicate in an Arraylist C#

Posted on 2014-01-23
2
Medium Priority
?
484 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 41

Accepted Solution

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


            ArrayList l = new ArrayList();

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

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 1000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

604 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