Javascript Program to Check Majority Element in a sorted array
Last Updated :
15 Feb, 2023
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 majority element (present more than n/2 times).
Examples:
Input: arr[] = {1, 2, 3, 3, 3, 3, 10}, x = 3
Output: True (x appears more than n/2 times in the given array)
Input: arr[] = {1, 1, 2, 4, 4, 4, 6, 6}, x = 4
Output: False (x doesn't appear more than n/2 times in the given array)
Input: arr[] = {1, 1, 1, 2, 2}, x = 1
Output: True (x appears more than n/2 times in the given array)
METHOD 1 (Using Linear Search)
Linearly search for the first occurrence of the element, once you find it (let at index i), check element at index i + n/2. If element is present at i+n/2 then return 1 else return 0.
Output:
4 appears more than 3 times in arr[]
Time Complexity: O(n)
Auxiliary Space: O(1)
As constant extra space is used.
METHOD 2 (Using Binary Search)
Use binary search methodology to find the first occurrence of the given number. The criteria for binary search is important here.
JavaScript
<script>
// Javascript Program to check for majority
// element in a sorted array */
// If x is present in arr[low...high]
// then returns the index of first
// occurrence of x, otherwise returns -1
function _binarySearch(arr, low, high, x)
{
if (high >= low) {
let mid = parseInt((low + high) / 2, 10);
//low + (high - low)/2;
// Check if arr[mid] is the first
// occurrence of x. arr[mid] is
// first occurrence if x is one of
// the following is true:
// (i) mid == 0 and arr[mid] == x
// (ii) arr[mid-1] < x and arr[mid] == x
if ((mid == 0 || x > arr[mid - 1]) && (arr[mid] == x))
return mid;
else if (x > arr[mid])
return _binarySearch(arr, (mid + 1), high, x);
else
return _binarySearch(arr, low, (mid - 1), x);
}
return -1;
}
// This function returns true if the x is
// present more than n/2 times in arr[]
// of size n
function isMajority(arr, n, x)
{
// Find the index of first occurrence
// of x in arr[]
let i = _binarySearch(arr, 0, n - 1, x);
// If element is not present at all,
// return false
if (i == -1)
return false;
// check if the element is present
// more than n/2 times
if (((i + parseInt(n / 2, 10)) <= (n - 1)) && arr[i + parseInt(n / 2, 10)] == x)
return true;
else
return false;
}
let arr = [ 1, 2, 3, 3, 3, 3, 10 ];
let n = arr.length;
let x = 3;
if (isMajority(arr, n, x) == true)
document.write(x + " appears more than " + parseInt(n / 2, 10) + " times in arr[]");
else
document.write(x + " does not appear more than " + parseInt(n / 2, 10) + " times in arr[]");
</script>
Output3 appears more than 3 times in arr[]
Time complexity: O(1)
Auxiliary Space: O(1)
Please refer complete article on Check for Majority Element in a sorted array for more details!
Similar Reads
Check for Majority Element in a sorted array Given an array arr of N elements, A majority element in an array arr of size N is an element that appears more than N/2 times in the array. The task is 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 i
15+ min read
Javascript Program for Check if an array is sorted and rotated Given an array of N distinct integers. The task is to write a program to check if this array is sorted and rotated counter-clockwise. A sorted array is not considered as sorted and rotated, i.e., there should at least one rotation.Examples: Input : arr[] = { 3, 4, 5, 1, 2 } Output : YES The above ar
3 min read
JavaScript Program to find the Nth smallest/largest element from an unsorted Array In this article, we will see how to find Nth largest and smallest element from an unsorted array. The Nth smallest/largest element from an unsorted array refers to the element that ranks at the Nth position when the array is sorted either in ascending (smallest) or descending (largest) order. It rep
4 min read
Javascript Program to Check if it is possible to sort the array after rotating it Given an array of size N, the task is to determine whether its possible to sort the array or not by just one shuffle. In one shuffle, we can shift some contiguous elements from the end of the array and place it in the front of the array.For eg: A = {2, 3, 1, 2}, we can shift {1, 2} from the end of t
3 min read
Javascript Program to Find median in row wise sorted matrix We are given a row-wise sorted matrix of size r*c, we need to find the median of the matrix given. It is assumed that r*c is always odd.Examples: Input : 1 3 5 2 6 9 3 6 9Output : Median is 5If we put all the values in a sorted array A[] = 1 2 3 3 5 6 6 9 9)Input: 1 3 4 2 5 6 7 8 9Output: Median is
4 min read
Check whether a given array is a k sorted array or not Given an array of n distinct elements. Check whether the given array is a k sorted array or not. A k sorted array is an array where each element is at most k distances away from its target position in the sorted array. For example, let us consider k is 2, an element at index 7 in the sorted array, c
12 min read