
- 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
Checking for majority element in a sorted array in JavaScript
Majority Element:
A majority element in an array arr of length l is an element that appears more than l/2 times and hence there is at most one such element.
We need to write a JavaScript function, say isMajority() that takes an array arr which is always sorted in the increasing order as the first argument.
The second argument of the function will be a number, for which we will search the array and return true if that number is the majority element or false otherwise.
For example −
If the input array and the number are −
const arr = [5, 5, 5, 12, 15]; const num = 5;
Then the output should be −
const output = true;
because 5 appears for 3 times which is greater than (5 / 2) = 2.5. (half of the length of the array).
It is given that the array is sorted and if there exists a majority element, it will always be the middle element because that number will have to span over at least more than half of the array.
We can use this logic to check if the given number is the majority element.
Example
The code for this will be −
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); if(arr[middle] === num){ return true; }else{ return false; }; }; console.log(isMajority(arr, num));
Output
And the output in the console will be −
true
- Related Articles
- Check for Majority Element in a sorted array in C++
- Check If a Number Is Majority Element in a Sorted Array in Python
- JavaScript Program for the Last duplicate element in a Sorted Array
- Does this array contain any majority element - JavaScript
- Finding the majority element of an array JavaScript
- JavaScript Program for Search an element in a sorted and rotated array
- Finding first unique element in sorted array in JavaScript
- Checking for uniqueness in an array in JavaScript
- JavaScript Program for Ceiling in a sorted array
- Finding the first unique element in a sorted array in JavaScript
- Nth smallest element in sorted 2-D array in JavaScript
- Finding smallest element in a sorted array which is rotated 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
