JavaScript Program for Finding the Majority Element of an Array
Last Updated :
16 Jul, 2024
Finding the majority element in an array is a common problem in computer science and data analysis. The majority element in an array is the element that appears more than n/2 times, where n is the length of the array. In other words, it's the element that occurs more frequently than any other element in the array.
Examples:
Input: arr=[2, 2, 3, 4, 2, 2, 5]
Output: 2
Explanation: 2 appears more than n/2 times
Input: arr=[2, 3, 3, 3, 2, 2, 3]
Output: 3
Explanation: 3 appears more than n/2 times
Method 1: Brute Force Approach:
Count the occurrences of each element and check if any element appears more than n/2 times.
Syntax:
for (let i = 0; i < n; i++) {
//implementation
if (count > n / 2) {
return arr[i];
}
}
Example: Below is the implementation of the above approach
JavaScript
function findMajority(arr)
{
const n = arr.length;
for (let i = 0; i < n; i++) {
let count = 0;
for (let j = 0; j < n; j++)
{
if (arr[i] === arr[j]) {
count++;
}
}
if (count > n / 2)
{
return arr[i];
}
}
return null;
}
const arr1 = [2, 2, 3, 4, 2, 2, 5];
console.log(findMajority(arr1));
Method 2: Sorting Approach:
Sort the array and the majority element will be the middle element (n/2-th element).
Syntax:
arr.sort((a, b) => a - b);
return arr[majorityIndex];
Example: Below is the implementation of the above approach
JavaScript
function findMajority(arr) {
arr.sort((a, b) => a - b);
const majorityIndex =
Math.floor(arr.length / 2);
return arr[majorityIndex];
}
const arr2 =
[2, 2, 3, 4, 2, 2, 5];
console.log(findMajority(arr2));
Method 3: Boyer-Moore Voting Algorithm:
This algorithm finds the majority element in linear time and constant space
Syntax:
let candidate = null;
let count = 0;
for (let num of arr) {
if (count === 0) {
candidate = num;
}
count += (num === candidate)
? 1 : -1;
}
return candidate;
Example: Below is the implementation of the above approach
JavaScript
function findMajority(arr) {
let candidate = null;
let count = 0;
for (let num of arr) {
if (count === 0) {
candidate = num;
}
count += (num === candidate)
? 1 : -1;
}
return candidate;
}
const arr3 = [2, 2, 3, 4, 2, 2, 5];
console.log(findMajority(arr3));
Method 4: Using a Hash Map
In this approach, we utilize a hash map (JavaScript object) to count the occurrences of each element in the array. By iterating through the array, we update the count for each element in the hash map. If any element's count exceeds n/2 during the iteration, we return that element as the majority element.
Example:
JavaScript
function findMajorityElement(arr) {
const elementCount = {};
const n = arr.length;
for (let num of arr) {
elementCount[num] = (elementCount[num] || 0) + 1;
if (elementCount[num] > n / 2) {
return num;
}
}
return null;
}
let arr1 = [2, 2, 3, 4, 2, 2, 5];
console.log(findMajorityElement(arr1));
let arr2 = [2, 3, 3, 3, 2, 2, 3];
console.log(findMajorityElement(arr2));
Similar Reads
JavaScript Program to Find all Elements that Appear More than n/k Times We are given an array that has n number of elements and an integer k, we have to return all the elements which will appear more than n/k times in the array using JavaScript. Examples:Input: arr = {4, 2, 4, 1, 4, 5, 5, 5}, k=3Output: {4 , 5}Explanation: The elements {4, 5} appears more than n/k i.e.
3 min read
JavaScript Program to Find the Majority Element that occurs more than N/2 Times We are given a JavaScript array and we need to find out the majority element in it which occurs more than N/2 times where N is the size of the array. Example:Input: arr[] = {3, 5, 8, 5, 4, 5, 7, 5, 5};Output: 5Explanation: Here, N = 9 and N/2 = 4, 5 appears 5 times in the array which is more than th
3 min read
Javascript Program to Check Majority Element in a sorted array Question: Write a function to find if a given integer x appears more than n/2 times in a sorted array of n integers. Basically, we need to write a function say isMajority() that takes an array (arr[] ), arrayâs size (n) and a number to be searched (x) as parameters and returns true if x is a majorit
3 min read
Find the Majority Element of an Array in PHP We will create a PHP array of integers i.e. $num1 or $num2 and find the element in an array that appears more than n/2 times, where n is the length of the array. The element that appears more than n/2 times will indicate the majority element in the array. These problems in PHP help users solve appli
4 min read
Javascript Program to Find closest number in array Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. Examples: Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9} Target number = 11 Output : 9 9 is closest to 11 in given array Input :arr[] = {2, 5, 6, 7, 8, 8, 9};
4 min read