Solved

Removing duplicate in an Arraylist C#

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

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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