0

I have an arraylist with few duplicate items. I need to know the count of each duplicated item. I am using 2.0 so cannot use linq.

I had posted a similar question earlier, but my question was not clear. Thanks Prady

3
  • Why did you tag it C#-3.0 if you are using 2.0? Commented Dec 30, 2010 at 17:54
  • sorry, i was changing it and you commented on it Commented Dec 30, 2010 at 17:55
  • Why would you use ArrayList in 2.0? Commented Dec 30, 2010 at 17:57

3 Answers 3

1

I've done something in the past. My solution was to loop through the ArrayList and store the counts in a dictionary. Then loop though the dictionary to display the results:

        ArrayList list = new ArrayList();
        list.Add(1);
        list.Add("test");
        list.Add("test");
        list.Add("test");
        list.Add(2);
        list.Add(3);
        list.Add(2);

        Dictionary<Object, int> itemCount = new Dictionary<object, int>();

        foreach (object o in list)
        {
            if (itemCount.ContainsKey(o))
                itemCount[o]++;
            else
                itemCount.Add(o, 1);
        }

        foreach (KeyValuePair<Object, int> item in itemCount)
        {
            if (item.Value > 1)
                Console.WriteLine(item.Key + " count: " + item.Value);
        }

Output:

test count: 3
2 count: 2

Edit Realized I used the var keyword which is not a 2.0 feature. Replaced it with KeyValuePair.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Chris... just one question though.. is there any way i can get to know the no of counts if i know the keyvalue. For instance i want to know the count of test, using something like find(test) will return 3
Yup. Using the code above. If you wanted to know the count of 2 you'd just do something like 'int count = itemCount[2]'.
0

Option 1: Sort the list and then count adjacently Equal items (requires you to override the Equals method for your class)

Option 2: Use your unique identifier (however you're defining two objects to be equal) as the key for a Dictionary and add each of your objects to that entry.

Comments

0

i needed something similar for a project a long time ago, and made a function for it

    static Dictionary<object, int> GetDuplicates(ArrayList list, out ArrayList uniqueList)
    {
        uniqueList = new ArrayList();
        Dictionary<object, int> dups = new Dictionary<object, int>();
        foreach (object o in list)
        {
            if (uniqueList.Contains(o))
                if (!dups.ContainsKey(o))
                    dups.Add(o, 2);
                else
                    dups[o]++;
            else
                uniqueList.Add(o);
        }
        return dups;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.