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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Suthish NairRank #92 · 22.8K reputationPosted Aug 24, 2011, 8:44 PM EDT
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();
Suthish NairRank #92 · 22.8K reputationPosted Aug 24, 2011, 8:16 PM EDT
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();
Tomas Westerlund38 reputationPosted Aug 24, 2011, 8:27 PM EDT
Suthish NairRank #92 · 22.8K reputationPosted Aug 26, 2011, 1:16 AM EDT
Tomas Westerlund38 reputationPosted Aug 26, 2011, 1:26 AM EDT