using System.Collections.Generic;
public static void Main()
Console.WriteLine("majority Number in the given array is:" + majorityNumberFromArray(new int[]{ 1, 3, 4, 5, 5, 5, 5}));
public static int majorityNumberFromArray(int[] unsortedArray)
if(unsortedArray == null || unsortedArray.Length ==0)
throw new Exception("the input array is either null or has no elements");
Dictionary<int, int> elementCountDict = new Dictionary<int, int>();
foreach (int x in unsortedArray)
if(elementCountDict.ContainsKey(x))
elementCountDict[x] += 1;
if(elementCountDict[x] > unsortedArray.Length/2)
elementCountDict.Add(x, 1);
throw new Exception("such number does not exist in the given array");