C# program to find majority element in an unsorted array
To find the majority element in an unsorted array using C#, you can use the dictionary object which will count the how many times value of the array idex is repeated. Here's an example program that finds the majority element in an unsorted array:
using System;
public class Program {
public static void Main(string[] args)
{
int[] numbers = { 2, 4, 3, 4, 4, 2, 14, 14, 6, 14, 14, 9, 9 };
int majorityElement = FindMajorityElement(numbers);
if (majorityElement != -1)
{
Console.WriteLine("The majority element is: " + majorityElement);
}
else
{
Console.WriteLine("No majority element found.");
}
Console.ReadKey();
}
public static int FindMajorityElement(int[] nums)
{
Dictionary keyValuePairs= new Dictionary();
foreach (int num in nums)
{
if (!keyValuePairs.ContainsKey(num))
keyValuePairs.Add(num, 1);
else if (keyValuePairs.ContainsKey(num))
{
keyValuePairs[num] = keyValuePairs[num] + 1;
}
}
int value = 0, key1 =0;
foreach(int key in keyValuePairs.Keys)
{
if (keyValuePairs[key] > value)
{
value = keyValuePairs[key];
key1 = key;
}
}
return key1;
}
}
Below is the output of the above program:
Explanation of C# Code
Here's a step-by-step explanation of what the code does:
1. Main Method
- The program starts execution in the
Main
method.
- An array of integers named
numbers
is defined with the values: { 2, 4, 3, 4, 4, 2, 14, 14, 6, 14, 14, 9, 9 }
.
- The
FindMajorityElement
method is called with the numbers
array as an argument. This method is responsible for finding the majority element.
- If the
FindMajorityElement
method returns a valid majority element (i.e., not -1
), the program prints: "The majority element is: <majorityElement>"
. Otherwise, it prints: "No majority element found."
.
- The program waits for a key press before exiting (
Console.ReadKey()
).
2. FindMajorityElement Method
This method takes an array of integers (nums
) as input and returns the majority element. Here's how it works:
Step 1: Counting Occurrences
- A
Dictionary
named keyValuePairs
is created to store each unique number in the array as a key and its count (number of occurrences) as the corresponding value.
- The program iterates through each number in the
nums
array:
- If the number is not already in the dictionary, it is added with a count of
1
.
- If the number is already in the dictionary, its count is incremented by
1
.
Step 2: Finding the Majority Element
- Two variables,
value
and key1
, are initialized to 0
. These will store the highest count and the corresponding number, respectively.
- The program iterates through the keys in the
keyValuePairs
dictionary:
- For each key, it checks if the associated count (value) is greater than the current
value
.
- If it is, the
value
is updated to this count, and key1
is updated to the current key (number).
- Finally, the method returns
key1
, which is the number with the highest count (the majority element).
Example Walkthrough
For the array { 2, 4, 3, 4, 4, 2, 14, 14, 6, 14, 14, 9, 9 }
:
- The dictionary
keyValuePairs
would look like this:
{
2: 2, // 2 appears 2 times
4: 3, // 4 appears 3 times
3: 1, // 3 appears 1 time
14: 4, // 14 appears 4 times
6: 1, // 6 appears 1 time
9: 2 // 9 appears 2 times
}
- The number
14
has the highest count (4
), so it is returned as the majority element.
Output
For the given array, the program would output:
"The majority element is: 14"
Key Points
- The program assumes that there is a single majority element (the one with the highest count).
- If multiple elements have the same highest count, the program will return the first one encountered during iteration.
- If the array is empty or no majority element is found, the program will return
-1
and print "No majority element found."
.