Difference between highest and least frequencies in an array | CPP code to find the difference between highest and least frequencies ; sort the array ; checking consecutive elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findDiff ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int count = 0 , max_count = 0 , min_count = n ; for ( int i = 0 ; i < ( n - 1 ) ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { count += 1 ; continue ; } else { max_count = max ( max_count , count ) ; min_count = min ( min_count , count ) ; count = 0 ; } } return ( max_count - min_count ) ; } int main ( ) { int arr [ ] = { 7 , 8 , 4 , 5 , 4 , 1 , 1 , 7 , 7 , 2 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findDiff ( arr , n ) << " STRNEWLINE " ; return 0 ; }