Hi,
I have an int array which looks like this: 3,5,6,1,1,1,3,7
How do you usually count how many occurrences of each number exists?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Suthish NairPosted Aug 25, 2011, 12:44 AM
int[] i = { 3, 5, 6, 1, 1, 1, 3, 7 };
var outval = from ii in i
group ii by ii into iiGroup
select new { iiGroup, iiCount = iiGroup.Count() };
foreach (var item in outval)
{
Console.WriteLine(item.iiGroup.Key + " - " + item.iiCount);
}
Console.ReadLine();
Tomas WesterlundPosted Aug 26, 2011, 5:26 AM
Suthish NairPosted Aug 26, 2011, 5:16 AM
Tomas WesterlundPosted Aug 25, 2011, 12:27 AM
Suthish NairPosted Aug 25, 2011, 12:16 AM
or something like this will help..
int[] i = { 3, 5, 6, 1, 1, 1, 3, 7 };
var outval = from ii in i
where ii == 3
select ii ;
Console.WriteLine(outval.Count());
Console.ReadLine();