- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Element Appearing More Than 25% In Sorted Array in C++
Suppose we have an array A. There are few elements. Some elements are common. We have to return an element that is appearing more than 25% spaces in the array. So if A = [1, 2, 4, 4, 4, 4, 5, 5, 6, 6, 7, 7], Here 4 has occurred four times. This is more than 25% of 12 (size of the array)
To solve this, we will follow these steps −
- Read elements and store their respective frequencies
- If the frequency is greater than 25% of the array size, then return the result.
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: int findSpecialInteger(vector<int>& arr) { int n = arr.size(); int req = n / 4; unordered_map <int, int> m; int ans = -1; for(int i = 0; i < n; i++){ m[arr[i]]++; if(m[arr[i]] > req)ans = arr[i]; } return ans; } }; main(){ Solution ob; vector<int> c = {1,2,4,4,4,4,5,5,6,6,7,7}; cout << ob.findSpecialInteger(c); }
Input
[1,2,4,4,4,4,5,5,6,6,7,7]
Output
4
- Related Articles
- Missing Element in Sorted Array in C++
- Single Element in a Sorted Array in C++
- MySQL query to find a value appearing more than once?
- k-th missing element in sorted array in C++
- Check for Majority Element in a sorted array in C++
- Maximum element in a sorted and rotated array in C++
- Find element in a sorted array whose frequency is greater than or equal to n/2 in C++.
- Array elements that appear more than once in C?
- Insert more than one element at once in a C# List
- C++ program to search an element in a sorted rotated array
- Find missing element in a sorted array of consecutive numbers in C++
- Finding first unique element in sorted array in JavaScript
- Why is it faster to process a sorted array than an unsorted array in C++?
- Find index of an extra element present in one sorted array in C++
- Count pairs in a sorted array whose sum is less than x in C++

Advertisements