Given an array of size n and a number k , find all elements that appear more than n / k times | C ++ implementation ; Function to find the number of array elements with frequency more than n / k times ; Calculating n / k ; Counting frequency of every element using Counter ; Traverse the map and print all the elements with occurrence more than n / k times ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printElements ( int arr [ ] , int n , int k ) { int x = n / k ; map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ arr [ i ] ] += 1 ; for ( int it = 0 ; it < mp . size ( ) ; it ++ ) { if ( mp [ it ] > x ) cout << ( it ) << endl ; } } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 2 , 3 , 5 , 4 , 2 , 2 , 3 , 1 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 4 ; printElements ( arr , n , k ) ; }