
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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. And 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.
Example
Following is the code −
const arr = [12, 5, 67, 12, 4, 12, 4, 12, 6, 12, 12]; const isMajority = arr => { let maxChar = -Infinity, maxCount = 1; // this loop determines the possible candidates for majorityElement for(let i = 0; i < arr.length; i++){ if(maxChar !== arr[i]){ if(maxCount === 1){ maxChar = arr[i]; }else{ maxCount--; }; }else{ maxCount++; }; }; // this loop actually checks for the candidate to be the majority element const count = arr.reduce((acc, val) => maxChar===val ? ++acc : acc, 0); return count > arr.length / 2; }; console.log(isMajority(arr));
Output
This will produce the following output in console −
true
- Related Articles
- Finding the majority element of an array JavaScript
- Checking for majority element in a sorted array in JavaScript
- JavaScript Determine the array having majority element and return TRUE if its in the same array
- Majority Element in C++
- Majority Element in Java
- Majority Element in Python
- Check for Majority Element in a sorted array in C++
- Majority Element II in C++
- Check If a Number Is Majority Element in a Sorted Array in Python
- Python – Test if all rows contain any common element with other Matrix
- Filtering array to contain palindrome elements in JavaScript
- Removing the odd occurrence of any number/element from an array in JavaScript
- Does Apple contain carbohydrates?
- Does guava contain citric acid?
- JavaScript - array undefined element count

Advertisements