Write a program in C# to display the number and frequency of number from given array in LINQ Query


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. class LinqExercise
  6. {
  7. static void Main(string[] args)
  8. {
  9. int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 4, 5, 2, 3, 10 };
  10. Console.Write("The numbers in the array are : \n");
  11. Console.Write("1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 4, 5, 2, 3, 10 \n");
  12.  
  13. var n = from x in arr
  14. group x by x into y
  15. select y;
  16. Console.WriteLine("\nThe number and the frequency are : \n");
  17. foreach (var arrno in n)
  18. {
  19. Console.WriteLine("Number " + arrno.Key + " appears " + arrno.Count() + " times");
  20. }
  21. Console.ReadLine();
  22. }
  23. }

Result

C# language techstudy.org

     





Related Topics




Popular Posts