C# - Count the number of occurences (frequency) of each number in the array


C# - Count the number of occurences (frequency) of each number in the array

CODE
using System; namespace SampleNS { class Program { static void Main(string[] args) { int[] numbers = new int[] { 1, 2, 1, 2, 1, 1, 1, 3, 4, 4, 6}; bool[] visited = new bool[numbers.Length]; for (int i = 0; i < numbers.Length; i++) { visited[i] = false; } Console.WriteLine("Frequency: "); for (int i = 0; i < numbers.Length; i++) { if (!visited[i]) { int count = 1; for (int j = i + 1; j < numbers.Length; j++) { if (numbers[i] == numbers[j]) { visited[j] = true; count++; } } Console.WriteLine("Frequency of " + numbers[i] + ": " + count); } } //OUTPUT //Frequency: //Frequency of 1: 5 //Frequency of 2: 2 //Frequency of 3: 1 //Frequency of 4: 2 //Frequency of 6: 1 Console.ReadKey(); } } }

Comments