
- 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
JavaScript Determine the array having majority element and return TRUE if its in the same array
We are required to write a JavaScript function that takes in an array of numbers with repetitive values and returns the number that appears for more than (n/2) number of times where n is the length of the array. If there is no such element in the array, our function should return false
Let's write the code for this function −
Example
const arr = [12, 5, 67, 12, 4, 12, 4, 12, 6, 12, 12]; const arr1 = [3, 565, 7, 23, 87, 23, 3, 65, 1, 3, 6, 7]; const findMajority = 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]; } 0else { 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(findMajority(arr)); console.log(findMajority(arr1));
Output
The output in the console will be −
true false
- Related Articles
- Finding the majority element of an array JavaScript
- Does this array contain any majority element - JavaScript
- Checking for majority element in a sorted array in JavaScript
- How to return true if the parent element contains the child element in JavaScript?
- If the element repeats, remove all its instances from array in JavaScript
- Compare and return True if an array is less than another array in Numpy
- Compare and return True if an array is greater than another array in Numpy
- Check If a Number Is Majority Element in a Sorted Array in Python
- Return a boolean array which is True where the string element in array starts with prefix in Python
- Return a boolean array which is True where the string element in array ends with suffix in Python
- Check for Majority Element in a sorted array in C++
- How to remove first array element in JavaScript and return it?
- How to remove last array element in JavaScript and return it?
- Comparing objects in JavaScript and return array of common keys having common values
- Return a new array with the same shape and type as given array in Numpy

Advertisements