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

Implementation:

## C++

 `// C++ code to find majority element in a ` `// sorted array ` `#include ` `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); ` `    ``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

 ` `

## Javascript

 ``

Output

`2`

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

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next