Check for Majority Element in a sorted array
Last Updated :
23 Jul, 2025
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 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 the element at index i + n/2. If the element is present at i+n/2 then return 1 else return 0.
C++
/* C++ Program to check for majority element in a sorted array */
#include<bits/stdc++.h>
using namespace std;
bool isMajority(int arr[], int n, int x)
{
int i;
/* get last index according to n (even or odd) */
int last_index = n % 2 ? (n / 2 + 1): (n / 2);
/* search for first occurrence of x in arr[]*/
for (i = 0; i < last_index; i++)
{
/* check if x is present and is present more than n/2
times */
if (arr[i] == x && arr[i + n / 2] == x)
return 1;
}
return 0;
}
/* Driver code */
int main()
{
int arr[] ={1, 2, 3, 4, 4, 4, 4};
int n = sizeof(arr)/sizeof(arr[0]);
int x = 4;
if (isMajority(arr, n, x))
cout << x <<" appears more than "<<
n/2 << " times in arr[]"<< endl;
else
cout <<x <<" does not appear more than" << n/2 <<" times in arr[]" << endl;
return 0;
}
// This code is contributed by shivanisinghss2110
C
/* C Program to check for majority element in a sorted array */
# include <stdio.h>
# include <stdbool.h>
bool isMajority(int arr[], int n, int x)
{
int i;
/* get last index according to n (even or odd) */
int last_index = n%2? (n/2+1): (n/2);
/* search for first occurrence of x in arr[]*/
for (i = 0; i < last_index; i++)
{
/* check if x is present and is present more than n/2
times */
if (arr[i] == x && arr[i+n/2] == x)
return 1;
}
return 0;
}
/* Driver program to check above function */
int main()
{
int arr[] ={1, 2, 3, 4, 4, 4, 4};
int n = sizeof(arr)/sizeof(arr[0]);
int x = 4;
if (isMajority(arr, n, x))
printf("%d appears more than %d times in arr[]",
x, n/2);
else
printf("%d does not appear more than %d times in arr[]",
x, n/2);
return 0;
}
Java
/* Program to check for majority element in a sorted array */
import java.io.*;
class Majority {
static boolean isMajority(int arr[], int n, int x)
{
int i, last_index = 0;
/* get last index according to n (even or odd) */
last_index = (n%2==0)? n/2: n/2+1;
/* search for first occurrence of x in arr[]*/
for (i = 0; i < last_index; i++)
{
/* check if x is present and is present more
than n/2 times */
if (arr[i] == x && arr[i+n/2] == x)
return true;
}
return false;
}
/* Driver function to check for above functions*/
public static void main (String[] args) {
int arr[] = {1, 2, 3, 4, 4, 4, 4};
int n = arr.length;
int x = 4;
if (isMajority(arr, n, x)==true)
System.out.println(x+" appears more than "+
n/2+" times in arr[]");
else
System.out.println(x+" does not appear more than "+
n/2+" times in arr[]");
}
}
/*This article is contributed by Devesh Agrawal*/
Python3
'''Python3 Program to check for majority element in a sorted array'''
def isMajority(arr, n, x):
# get last index according to n (even or odd) */
last_index = (n//2 + 1) if n % 2 != 0 else (n//2)
# search for first occurrence of x in arr[]*/
for i in range(last_index):
# check if x is present and is present more than n / 2 times */
if arr[i] == x and arr[i + n//2] == x:
return 1
# Driver program to check above function */
arr = [1, 2, 3, 4, 4, 4, 4]
n = len(arr)
x = 4
if (isMajority(arr, n, x)):
print ("% d appears more than % d times in arr[]"
%(x, n//2))
else:
print ("% d does not appear more than % d times in arr[]"
%(x, n//2))
# This code is contributed by shreyanshi_arun.
C#
// C# Program to check for majority
// element in a sorted array
using System;
class GFG {
static bool isMajority(int[] arr,
int n, int x)
{
int i, last_index = 0;
// Get last index according to
// n (even or odd)
last_index = (n % 2 == 0) ? n / 2 :
n / 2 + 1;
// Search for first occurrence
// of x in arr[]
for (i = 0; i < last_index; i++) {
// Check if x is present and
// is present more than n/2 times
if (arr[i] == x && arr[i + n / 2] == x)
return true;
}
return false;
}
// Driver code
public static void Main()
{
int[] arr = { 1, 2, 3, 4, 4, 4, 4 };
int n = arr.Length;
int x = 4;
if (isMajority(arr, n, x) == true)
Console.Write(x + " appears more than " +
n / 2 + " times in arr[]");
else
Console.Write(x + " does not appear more than " +
n / 2 + " times in arr[]");
}
}
// This code is contributed by Sam007
JavaScript
<script>
// Javascript Program to check for majority
// element in a sorted array
function isMajority(arr, n, x)
{
let i, last_index = 0;
// Get last index according to
// n (even or odd)
last_index = (n % 2 == 0) ?
parseInt(n / 2, 10) : parseInt(n / 2, 10) + 1;
// Search for first occurrence
// of x in arr[]
for (i = 0; i < last_index; i++) {
// Check if x is present and
// is present more than n/2 times
if (arr[i] == x && arr[i +
parseInt(n / 2, 10)] == x)
return true;
}
return false;
}
let arr = [ 1, 2, 3, 4, 4, 4, 4 ];
let n = arr.length;
let x = 4;
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>
PHP
<?php
// PHP Program to check for
// majority element in a
// sorted array
// function returns majority
// element in a sorted array
function isMajority($arr, $n, $x)
{
$i;
// get last index according
// to n (even or odd)
$last_index = $n % 2? ($n / 2 + 1): ($n / 2);
// search for first occurrence
// of x in arr[]
for ($i = 0; $i < $last_index; $i++)
{
// check if x is present and
// is present more than n/2
// times
if ($arr[$i] == $x && $arr[$i + $n / 2] == $x)
return 1;
}
return 0;
}
// Driver Code
$arr = array(1, 2, 3, 4, 4, 4, 4);
$n = sizeof($arr);
$x = 4;
if (isMajority($arr, $n, $x))
echo $x, " appears more than "
, floor($n / 2), " times in arr[]";
else
echo $x, "does not appear more than "
, floor($n / 2), "times in arr[]";
// This code is contributed by Ajit
?>
Output4 appears more than 3 times in arr[]
Time Complexity: O(n)
Auxiliary Space: O(1)
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.
C++
// C++ program to check for majority
// element in a sorted array
#include<bits/stdc++.h>
using namespace std;
// If x is present in arr[low...high]
// then returns the index of first
// occurrence of x, otherwise returns -1
int _binarySearch(int arr[], int low,
int high, int x);
// This function returns true if the x
// is present more than n/2 times in
// arr[] of size n
bool isMajority(int arr[], int n, int x)
{
// Find the index of first occurrence
// of x in arr[]
int 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 + n / 2) <= (n - 1)) &&
arr[i + n / 2] == x)
return true;
else
return false;
}
// If x is present in arr[low...high] then
// returns the index of first occurrence
// of x, otherwise returns -1
int _binarySearch(int arr[], int low,
int high, int x)
{
if (high >= low)
{
int mid = (low + high)/2; /*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;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 3, 3, 3, 10 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 3;
if (isMajority(arr, n, x))
cout << x << " appears more than "
<< n / 2 << " times in arr[]"
<< endl;
else
cout << x << " does not appear more than"
<< n / 2 << " times in arr[]" << endl;
return 0;
}
// This code is contributed by shivanisinghss2110
C
/* C Program to check for majority element in a sorted array */
# include <stdio.h>
# include <stdbool.h>
/* If x is present in arr[low...high] then returns the index of
first occurrence of x, otherwise returns -1 */
int _binarySearch(int arr[], int low, int high, int x);
/* This function returns true if the x is present more than n/2
times in arr[] of size n */
bool isMajority(int arr[], int n, int x)
{
/* Find the index of first occurrence of x in arr[] */
int 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 + n/2) <= (n -1)) && arr[i + n/2] == x)
return true;
else
return false;
}
/* If x is present in arr[low...high] then returns the index of
first occurrence of x, otherwise returns -1 */
int _binarySearch(int arr[], int low, int high, int x)
{
if (high >= low)
{
int mid = (low + high)/2; /*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;
}
/* Driver program to check above functions */
int main()
{
int arr[] = {1, 2, 3, 3, 3, 3, 10};
int n = sizeof(arr)/sizeof(arr[0]);
int x = 3;
if (isMajority(arr, n, x))
printf("%d appears more than %d times in arr[]",
x, n/2);
else
printf("%d does not appear more than %d times in arr[]",
x, n/2);
return 0;
}
Java
/* Java Program to check for majority element in a sorted array */
import java.io.*;
class Majority {
/* If x is present in arr[low...high] then returns the index of
first occurrence of x, otherwise returns -1 */
static int _binarySearch(int arr[], int low, int high, int x)
{
if (high >= low)
{
int mid = (low + high)/2; /*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 */
static boolean isMajority(int arr[], int n, int x)
{
/* Find the index of first occurrence of x in arr[] */
int 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 + n/2) <= (n -1)) && arr[i + n/2] == x)
return true;
else
return false;
}
/*Driver function to check for above functions*/
public static void main (String[] args) {
int arr[] = {1, 2, 3, 3, 3, 3, 10};
int n = arr.length;
int x = 3;
if (isMajority(arr, n, x)==true)
System.out.println(x + " appears more than "+
n/2 + " times in arr[]");
else
System.out.println(x + " does not appear more than " +
n/2 + " times in arr[]");
}
}
/*This code is contributed by Devesh Agrawal*/
Python3
'''Python3 Program to check for majority element in a sorted array'''
# This function returns true if the x is present more than n / 2
# times in arr[] of size n */
def isMajority(arr, n, x):
# Find the index of first occurrence of x in arr[] */
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 + n//2) <= (n -1)) and arr[i + n//2] == x:
return True
else:
return False
# If x is present in arr[low...high] then returns the index of
# first occurrence of x, otherwise returns -1 */
def _binarySearch(arr, low, high, x):
if high >= low:
mid = (low + high)//2 # 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 or x > arr[mid-1]) and (arr[mid] == x):
return mid
elif x > arr[mid]:
return _binarySearch(arr, (mid + 1), high, x)
else:
return _binarySearch(arr, low, (mid -1), x)
return -1
# Driver program to check above functions */
arr = [1, 2, 3, 3, 3, 3, 10]
n = len(arr)
x = 3
if (isMajority(arr, n, x)):
print ("% d appears more than % d times in arr[]"
% (x, n//2))
else:
print ("% d does not appear more than % d times in arr[]"
% (x, n//2))
# This code is contributed by shreyanshi_arun.
C#
// C# Program to check for majority
// element in a sorted array */
using System;
class GFG {
// If x is present in arr[low...high]
// then returns the index of first
// occurrence of x, otherwise returns -1
static int _binarySearch(int[] arr, int low,
int high, int x)
{
if (high >= low) {
int mid = (low + high) / 2;
//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
static bool isMajority(int[] arr, int n, int x)
{
// Find the index of first occurrence
// of x in arr[]
int 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 + n / 2) <= (n - 1)) &&
arr[i + n / 2] == x)
return true;
else
return false;
}
//Driver code
public static void Main()
{
int[] arr = { 1, 2, 3, 3, 3, 3, 10 };
int n = arr.Length;
int x = 3;
if (isMajority(arr, n, x) == true)
Console.Write(x + " appears more than " +
n / 2 + " times in arr[]");
else
Console.Write(x + " does not appear more than " +
n / 2 + " times in arr[]");
}
}
// This code is contributed by Sam007
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(log n)
Auxiliary Space: O(1)
Algorithmic Paradigm: Divide and Conquer
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem