LINQ Query to display the array elements and the number of times they occur

Ddd
Posted by Ddd under LINQ category on | Points: 40 | Views : 3214
This is the code to display the elements of an array and the number of times they occur using System;

Take a Console Application and write the code in Main method

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplicationlinq
{
class Program
{
static void Main(string[] args)
{
int[] arr = { 10, 20, 30, 30, 10 };
//use the Group By function with a lambda
//expression
var q = arr.GroupBy(a => a);

//use the Generic interface IGrouping
foreach (IGrouping<int, int> z in q)
{
Console.WriteLine(z.Key + "---" + z.Count());
}
}
}
}

output:
10--2
20--1
30--2

Comments or Responses

Login to post response