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
Checking for majority element in a sorted array in JavaScript
A majority element in an array of length l is an element that appears more than l/2 times. In a sorted array, if a majority element exists, it must occupy the middle position since it spans over more than half the array.
Problem Definition
We need to write a JavaScript function isMajority() that takes a sorted array and a number, returning true if that number is the majority element, false otherwise.
Key Insight for Sorted Arrays
In a sorted array, if there exists a majority element, it will always be the middle element. This is because a majority element must appear more than half the time, so it necessarily occupies the center position.
Basic Implementation
const arr = [5, 5, 5, 12, 15];
const num = 5;
const isMajority = (arr = [], num = 1) => {
const { length } = arr;
if (!length) {
return false;
}
const middle = Math.floor(length / 2);
return arr[middle] === num;
};
console.log(isMajority(arr, num));
console.log(isMajority([1, 2, 3, 3, 3], 3));
console.log(isMajority([1, 1, 2, 2, 2], 1));
true true false
Enhanced Implementation with Count Verification
For additional validation, we can verify the actual count using binary search to find the first and last occurrence:
const isMajorityWithCount = (arr, num) => {
if (arr.length === 0) return false;
const middle = Math.floor(arr.length / 2);
if (arr[middle] !== num) return false;
// Find first occurrence
let left = 0, right = arr.length - 1;
let first = -1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] === num) {
first = mid;
right = mid - 1;
} else if (arr[mid] < num) {
left = mid + 1;
} else {
right = mid - 1;
}
}
// Find last occurrence
left = 0;
right = arr.length - 1;
let last = -1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] === num) {
last = mid;
left = mid + 1;
} else if (arr[mid] < num) {
left = mid + 1;
} else {
right = mid - 1;
}
}
const count = last - first + 1;
return count > arr.length / 2;
};
console.log(isMajorityWithCount([5, 5, 5, 12, 15], 5));
console.log(isMajorityWithCount([1, 2, 2, 2, 3], 2));
true false
Test Cases
const testCases = [
{ arr: [5, 5, 5, 12, 15], num: 5, expected: true },
{ arr: [1, 2, 2, 2, 3], num: 2, expected: false },
{ arr: [7, 7, 7, 7], num: 7, expected: true },
{ arr: [1, 1, 2, 2], num: 1, expected: false },
{ arr: [], num: 5, expected: false }
];
testCases.forEach((test, index) => {
const result = isMajority(test.arr, test.num);
console.log(`Test ${index + 1}: ${result === test.expected ? 'PASS' : 'FAIL'} - Got ${result}, Expected ${test.expected}`);
});
Test 1: PASS - Got true, Expected true Test 2: PASS - Got true, Expected false Test 3: PASS - Got true, Expected true Test 4: PASS - Got false, Expected false Test 5: PASS - Got false, Expected false
Time Complexity
| Method | Time Complexity | Space Complexity |
|---|---|---|
| Middle Element Check | O(1) | O(1) |
| With Count Verification | O(log n) | O(1) |
Conclusion
For sorted arrays, checking the middle element provides an O(1) solution to identify majority elements. The enhanced version with binary search offers additional verification while maintaining efficient O(log n) performance.
