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 Find k Most Occurrences in the Given Array K most occurrences in an array refer to finding the K unique elements that appear the most frequently within the given array, where K is a specified integer. These are the elements with the highest frequencies in the array.Example:Input: arr[] = {3, 1, 4, 4, 5, 2, 6, 1}, K = 2Output: 4 1Explanation:
6 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
Find the Middle Element of an Array or List Given an array or a list of elements. The task is to find the middle element of given array or list of elements. If the array size is odd, return the single middle element. If the array size is even, return the two middle elements. ExampleInput: arr = {1, 2, 3, 4, 5}Output: 3 Input: arr = {7, 8, 9,
9 min read
Java Program to Print the Smallest Element in an Array Java provides a data structure, the array, which stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. Example: arr1[] = {2 , -1 , 9 , 10} output : -1 arr2[] = {0, -10, -13, 5} output : -13 We need to find and print the smallest value
3 min read
Java Program to Print the kth Element in the Array We need to print the element at the kth position in the given array. So we start the program by taking input from the user about the size of an array and then all the elements of that array. Now by entering the position k at which you want to print the element from the array, the program will print
2 min read
Javascript Program for Range Queries for Frequencies of array elements Given an array of n non-negative integers. The task is to find frequency of a particular element in the arbitrary range of array[]. The range is given as positions (not 0 based indexes) in array. There can be multiple queries of given type. Examples: Input : arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11}; lef
2 min read