Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Sum of elements in an array with frequencies greater than or equal to that element
Next article icon

Find element in a sorted array whose frequency is greater than or equal to n/2.

Last Updated : 24 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a sorted array of length n, find the number in array that appears more than or equal to n/2 times. It is given that such element always exists.

Examples: 

Input :  2 3 3 4  Output : 3    Input :  3 4 5 5 5   Output : 5    Input : 1 1 1 2 3  Output : 1

To find that number, we traverse the array and check the frequency of every element in array if it is greater than or equals to n/2 but it requires extra space and time complexity will be O(n). 

But we can see that the if there is number that comes more than or equal to n/2 times in a sorted array, then that number must be present at the position n/2 i.e. a[n/2].

Implementation:

C++




// C++ code to find majority element in a
// sorted array
#include <iostream>
using namespace std;
  
int findMajority(int arr[], int n)
{
    return arr[n / 2];
}
  
int main()
{
    int arr[] = { 1, 2, 2, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findMajority(arr, n);
    return 0;
}
 
 

Java




// Java code to find majority element in a
// sorted array
public class Test {
  
    public static int findMajority(int arr[], int n)
    {
        return arr[n / 2];
    }
    public static void main(String args[])
    {
        int arr[] = { 1, 2, 2, 3 };
        int n = arr.length;
        System.out.println(findMajority(arr, n));
    }
}
 
 

Python 3




# Python 3 code to find
# majority element in a
# sorted array
  
def findMajority(arr, n):
  
    return arr[int(n / 2)]
  
# Driver Code
arr = [1, 2, 2, 3]
n = len(arr) 
print(findMajority(arr, n))
  
# This code is contributed by Smitha.
 
 

C#




// C# code to find majority element in a
// sorted array
using System;
  
public class GFG {
  
    public static int findMajority(int []arr, int n)
    {
        return arr[n / 2];
    }
      
    // Driver code
    public static void Main()
    {
          
        int []arr = { 1, 2, 2, 3 };
        int n = arr.Length;
          
        Console.WriteLine(findMajority(arr, n));
    }
}
  
// This code is contributed by vt_m. 
 
 

PHP




<?php
// PHP code to find majority 
// element in a sorted array
  
function findMajority($arr, $n)
{
    return $arr[intval($n / 2)];
}
  
    // Driver Code
    $arr = array(1, 2, 2, 3);
    $n = count($arr);
    echo findMajority($arr, $n);     
      
// This code is contributed by Sam007
?>
 
 

Javascript




<script>
  
// Javascript code to find majority element in a
// sorted array
  
function findMajority(arr,  n)
{
    return arr[(Math.floor(n / 2))];
}
  
// driver code 
  
    let arr = [ 1, 2, 2, 3 ];
    let n = arr.length;
    document.write(findMajority(arr, n));
  
</script>
 
 
Output
2

Time Complexity : O(1)
Auxiliary Space: O(1)

Related Articles : 
Majority element in an unsorted array 
Check for majority element in a sorted array

 



Next Article
Sum of elements in an array with frequencies greater than or equal to that element
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • Arrays
  • DSA
  • Searching
Practice Tags :
  • Arrays
  • Searching

Similar Reads

  • Sum of elements in an array with frequencies greater than or equal to that element
    Given an array arr[] of N integers. The task is to find the sum of the elements which have frequencies greater than or equal to that element in the array. Examples: Input: arr[] = {2, 1, 1, 2, 1, 6} Output: 3 The elements in the array are {2, 1, 6} Where, 2 appear 2 times which is greater than equal
    9 min read
  • Find the frequency of each element in a sorted array
    Given a sorted array, arr[] consisting of N integers, the task is to find the frequencies of each array element. Examples: Input: arr[] = {1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10} Output: Frequency of 1 is: 3 Frequency of 2 is: 1 Frequency of 3 is: 2 Frequency of 5 is: 2 Frequency of 8 is: 3 Frequ
    10 min read
  • Maximum element in an array which is equal to its frequency
    Given an array of integers arr[] of size N, the task is to find the maximum element in the array whose frequency equals to it's value Examples: Input: arr[] = {3, 2, 2, 3, 4, 3} Output: 3 Frequency of element 2 is 2 Frequency of element 3 is 3 Frequency of element 4 is 1 2 and 3 are elements which h
    11 min read
  • Find frequency of each element in a limited range array in less than O(n) time
    Given a sorted array arr[] of positive integers, the task is to find the frequency for each element in the array. Assume all elements in the array are less than some constant M Note: Do this without traversing the complete array. i.e. expected time complexity is less than O(n) Examples: Input: arr[]
    10 min read
  • Find element with highest frequency in given nested Array
    Given an array arr[] of N integers. The task is to create a frequency array freq[] of the given array arr[] and find the maximum element of the frequency array. If two elements have the same frequency in the array freq[], then return the element which has a smaller value. Examples: Input: arr[] = {1
    8 min read
  • Count elements less than or equal to a given value in a sorted rotated array
    Given a sorted array of n distinct integers rotated at some point. Given a value x. The problem is to count all the elements in the array which are less than or equal to x. Examples: Input : arr[] = {4, 5, 8, 1, 3}, x = 6 Output : 4 Input : arr[] = {6, 10, 12, 15, 2, 4, 5}, x = 14 Output : 6 Naive A
    15 min read
  • Find elements larger than half of the elements in an array | Set 2
    Given an array arr[] consisting of N positive integers, the task is to find the elements which are greater than at least half of the array elements. Examples: Input: arr[] = {1, 6, 3, 4}Output: 4 6Explanation:Size of the array is 4. The elements which are greater than atleast N/2(= 4/2 = 2) elements
    7 min read
  • Find array elements with frequencies in range [l , r]
    Given an array of integers, find the elements from the array whose frequency lies in the range [l, r]. Examples: Input : arr[] = { 1, 2, 3, 3, 2, 2, 5 } l = 2, r = 3 Output : 2 3 3 2 2 Approach : Take a hash map, which will store the frequency of all the elements in the array.Now, traverse once agai
    9 min read
  • Fill an array based on frequency where elements are in range from 0 to n-1
    Given an array of positive integers with duplicate allowed. The array contains elements from range 0 to n-1. The task is to fill the array such that arr[i] contains the frequency of i. Examples : Input : arr[] = {1, 4, 3, 4, 1, 1, 4, 4, 4, 7} Output : arr[] = {0, 3, 0, 1, 5, 0, 0, 1, 0, 0} Here 0 ap
    5 min read
  • Count array elements whose highest power of 2 less than or equal to that number is present in the given array
    Given an array arr[] consisting of N positive integers, the task is to find the number of array elements whose highest power of 2 less than or equal to that number is present in the array. Examples: Input: arr[] = {3, 4, 6, 9}Output: 2Explanation:There are 2 array elements (4 and 6), whose highest p
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences