1

I have a arraylist which has values some of them are repeated. I need the count of the repeated values. Is this possible in c#?

7
  • Do you want "classic" C# syntax or is using Linq OK? (i.e. are you using 3.5?) Commented Dec 30, 2010 at 16:01
  • Do you want a count of items that are repeated or a total number of repeats? Meaning, foo is in the list three times, is that 1 item that is repeated or is it 2 repeats? Commented Dec 30, 2010 at 16:04
  • 1
    I need in pure c #, its no of times foo is repeated Commented Dec 30, 2010 at 16:06
  • If foo is there 3 times then i would need it say 3. Commented Dec 30, 2010 at 16:54
  • @Prady, I think I got what you need Commented Dec 30, 2010 at 17:01

8 Answers 8

3

here is a great post how to do it with LINQ

var query =
    from c in arrayList
    group c by c into g
    where g.Count() > 1
    select new { Item = g.Key,  ItemCount = g.Count()};

foreach (var item in query)
{
    Console.WriteLine("Country {0} has {1} cities", item.Item , item.ItemCount );
}
Sign up to request clarification or add additional context in comments.

1 Comment

he says he doesn't use linq but that's exactly what I was going to enter.
3

If your object has equals method correctly overrided just call Distinct() from System.Linq namespace on it

It requires the ArrayList to be homogeneous and calling Cast<YourType>() before Distinct().

Then subtract the length of arrayList from the Distinct sequence.

arraList.Count - arraList.Cast<YourType>().Distinct().Count()

it will throw exception if your items in arrayList is not of type YourType, and if you use OfType<YourType> it filters items to objects of type YourType.

but if you want the count of each repeated item, this is not your answer.

2 Comments

Distinct removed dups, not found ;)
It's better to use OfType<T> rather than Cast<T> because first will return only proper objects while your - will raise an error. Anyway, it's much better to use generic collections
3
public Dictionary<T,int> CountOccurences<T>(IEnumerable<T> items) {
  var occurences = new Dictionary<T,int>();
  foreach(T item in items) {
    if(occurences.ContainsKey(item)) {
      occurences[item]++;
    } else {
      occurences.Add(item, 1);
    }
  }
  return occurences;
}

2 Comments

+1 Simple and easy to understand. Although I think you meant occurences.Add, not occurences.Put.
Yeah, I don't know why so many people insist using Linq for each and every problem. That's pretty unredable most of the time.
2
myList.GroupBy(i => i).Count(g => g.Count() > 1)

and if you specifically need ArrayList

ArrayList arrayList = new ArrayList(new[] { 1, 1, 2, 3, 4, 4 });
Console.WriteLine(arrayList.ToArray().GroupBy(i => i).Count(g => g.Count() > 1));

Based on comments by poster

ArrayList arrayList = new ArrayList(new[] { 1, 1, 2, 3, 4, 4 });
Console.WriteLine(arrayList.ToArray().Count(i => i == 4));

7 Comments

-1: It doesn't show the correct number of duplicates and also the example shown is with list, not with ArrayList.
+1 wouldn't you need something like this: .GroupBy(i => i).Count(g => g == "foo" && g.Count() > 1) in addition?
@hunter depends on how you read the question. I'm still not sure I quite understand what he's looking for.
@hunter, I don't know that reference.
@Yuriy, try to add another "4" into the array. Your LINQ query will return again "2" while "the repeated values" are three.
|
1
int countDup = ArrayList1.Count - ArrayList1.OfType<object>().Distinct().Count();

2 Comments

So if one item is repeated 5 times, you would return 4 repeated items?
change the Length to Count dude :-)
0
var items = arrayList.Cast<object>()
  .GroupBy(o => o)
  .Select(g => new { Item = g, Count = g.Count() })
  .ToList();

each item of result list will have two properties: Item - source item Count - count in source list

Comments

0

You can acomplish this many ways. The first that comes to me would be to group by the values within your array list, and only return the grouping counts that are over 1.

ArrayList al = new ArrayList();
al.Add("a");
al.Add("b");
al.Add("c");
al.Add("f");
al.Add("a");
al.Add("f");

int count = al.ToArray().GroupBy(q => q).Count(q=>q.Count()>1);

count will return the value of 2 as a and f are duplicated.

Comments

0

You could sort it, then it becomes very easy.

Edit: sorting becomes a moot point when done this way.

Arraylist myList = new ArrayList();

myList = someStuff;
Dictionary<object, int> counts = new Dictionary<object,int>();
foreach (object item in myList)
{
    if (!counts.ContainsKey(item))
    {
       counts.Add(item,1);
    }
    else
    {
       counts[item]++;
    }

}

Edit:

Some minor things might vary (not certain about some of my square braces, I'm a little rusty with c#) but the concept should withstand scrutiny.

5 Comments

I don't get why this has to be sorted.
Because if you don't sort it, you would have to scan the list for each object(foreach within a foreach), this makes it much faster.
suppose there is foo 3 times, fee 2 times, how do i get to see foo=3 and fee=2 . Pls forgive me if the question was very basic in nature
I was reading your code how I thought you should do it I guess. Shouldn't you just increment counts[object] if it exists and make it 1 if it doesn't while looping through the list once?
@Prady: You could then loop through the dictionary and print off the values. @Blankasaurus: You are quite right, the dictionary can essentially act as my temp object, that was stupid of me. I'll fix it now.

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.