Write a program in C# Sharp to display the number and frequency of number from giving array

  • برمجة سي شارب
  • برمجة

Write a program in C# Sharp to display the number and frequency of number from giving array.

Expected Output :
The number and the Frequency are :
Number 5 appears 3 times
Number 9 appears 2 times
Number 1 appears 1 times

الأجوبة

using System;
using System.Linq;
using System.Collections.Generic;
class LinqExercise4
{
    static void Main(string[] args)
    {
         int[] arr1 = new int[] { 5, 9, 1, 2, 3, 7, 5, 6, 7, 3, 7, 6, 8, 5, 4, 9, 6, 2 };  
         Console.Write("\nLINQ : Display the number and frequency of number from given array : \n"); 
         Console.Write("---------------------------------------------------------------------\n");
         Console.Write("The numbers in the array  are : \n");
         Console.Write(" 5, 9, 1, 2, 3, 7, 5, 6, 7, 3, 7, 6, 8, 5, 4, 9, 6, 2\n");
			
		var n = from x in arr1  
				group x by x into y  
				select y;  
				Console.WriteLine("\nThe number and the Frequency are : \n"); 
			    foreach (var arrNo in n)  
				{  
					Console.WriteLine("Number "+arrNo.Key + " appears " + arrNo.Count()+" times");  
				} 
        Console.WriteLine("\n");				
    }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...