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> Output2 Time Complexity : O(1)Auxiliary Space: O(1) Related Articles : Majority element in an unsorted array Check for majority element in a sorted array Comment More infoAdvertise with us Next Article Find element in a sorted array whose frequency is greater than or equal to n/2. _ _amit_ Improve Article Tags : Searching DSA Arrays Practice Tags : ArraysSearching 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 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 MNote: Do this without traversing the complete array. i.e. expected time complexity is less than O(n)Examples: Input: arr[] = 10 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 Like