/*https://www.geeksforgeeks.org/smallest-element-repeated-exactly-k-times-not-limited-small-range/ Smallest element repeated exactly ‘k’ times (not limited to small range) */ #include<bits/stdc++.h> using namespace std; int no_of_chars = 10; int smallrepeat(int s[],int k, int n) { int hash[no_of_chars] = {0}; //Storing the frequency of occurances. for(int i=0;i<n;i++) hash[s[i]]++; //Finding smallest element repeated exactly 'k' times int repeatmore = 9; for(int i=0;i<n;i++){ if(hash[s[i]] && (hash[s[i]] == k) && (s[i] < repeatmore)) repeatmore = s[i]; } return repeatmore; } int main() { int s[] = { 2, 2, 1, 3, 1 }; int n = sizeof(s)/sizeof(s[0]); int k = 2; cout<<"Smallest element repeated exactly ‘k’ times :"<<smallrepeat(s,k,n)<<endl; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)