Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Does this array contain any majority element - JavaScript
Given an array of numbers, any element of the array will be a majority element if that element appears more than array length's 1/2 times in the array.
For example, if the length of array is 7, then if there's any element in the array that appears for at least 4 number of times, it will be considered a majority. It's quite apparent that any particular array can have at most one majority element.
We are required to write a JavaScript function that takes in an array of numbers with repetitive values and returns true if there exists a majority element in the array. If there is no such element in the array, our function should return false.
Boyer-Moore Voting Algorithm
The most efficient approach is the Boyer-Moore Voting Algorithm, which finds the majority element in O(n) time and O(1) space.
const arr = [12, 5, 67, 12, 4, 12, 4, 12, 6, 12, 12];
const isMajority = arr => {
let maxChar = -Infinity, maxCount = 1;
// Phase 1: Find potential candidate for majority element
for(let i = 0; i maxChar === val ? ++acc : acc, 0);
return count > arr.length / 2;
};
console.log(isMajority(arr));
console.log(`Array length: ${arr.length}, Required count: ${Math.floor(arr.length / 2) + 1}`);
true Array length: 11, Required count: 6
Alternative Approach: Using Map
A simpler but less efficient approach uses a Map to count occurrences:
const findMajoritySimple = arr => {
const countMap = new Map();
const threshold = arr.length / 2;
// Count occurrences of each element
for(let num of arr) {
countMap.set(num, (countMap.get(num) || 0) + 1);
if(countMap.get(num) > threshold) {
return true;
}
}
return false;
};
const testArr = [3, 3, 4, 2, 4, 4, 2, 4, 4];
console.log(findMajoritySimple(testArr));
// Count each element
const countMap = new Map();
for(let num of testArr) {
countMap.set(num, (countMap.get(num) || 0) + 1);
}
console.log("Element counts:", Object.fromEntries(countMap));
true
Element counts: { '3': 2, '4': 5, '2': 2 }
Testing Different Cases
// Test cases
const testCases = [
[2, 2, 1, 1, 1, 2, 2], // 2 appears 4 times out of 7
[3, 2, 3], // 3 appears 2 times out of 3
[2, 2, 1, 3], // No majority element
[1] // Single element is majority
];
testCases.forEach((arr, index) => {
const result = isMajority(arr);
console.log(`Test ${index + 1}: [${arr}] => ${result}`);
});
Test 1: [2,2,1,1,1,2,2] => true Test 2: [3,2,3] => true Test 3: [2,2,1,3] => false Test 4: [1] => true
Comparison
| Approach | Time Complexity | Space Complexity | Advantage |
|---|---|---|---|
| Boyer-Moore | O(n) | O(1) | Most efficient, constant space |
| Map Counting | O(n) | O(n) | Easier to understand |
Conclusion
The Boyer-Moore Voting Algorithm provides the most efficient solution for finding majority elements. It uses two phases: finding a candidate and verifying if it's truly a majority element with more than n/2 occurrences.
