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

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].

## 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; ` `} ` |

*chevron_right*

*filter_none*

## 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)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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 ` `?> ` |

*chevron_right*

*filter_none*

Output:

2

Time Complexity : O(1)

**Related Articles : **

Majority element in an unsorted array

Check for majority element in a sorted array

This article is contributed by **Amit Kumar**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- First strictly greater element in a sorted array in Java
- Sum of elements in an array with frequencies greater than or equal to that element
- Adding elements of an array until every element becomes greater than or equal to k
- Find the element having different frequency than other array elements
- Find the number of elements greater than k in a sorted array
- Next Greater Frequency Element
- Find frequency of each element in a limited range array in less than O(n) time
- Find closest greater value for every element in array
- Find Equal (or Middle) Point in a sorted array with duplicates
- Find first and last positions of an element in a sorted array
- Find the element that appears once in a sorted array
- Longest Subarray with first element greater than or equal to Last element
- Find the only repeating element in a sorted array of size n
- Find the minimum element in a sorted and rotated array
- Find the Kth smallest element in the sorted generated array