// intialize frequency container void countFreq(int a[], int n, unordered_map<int, int> &hm) { // Insert elements and their // frequencies in hash map. for (int i = 0; i < n; i++) hm[a[i]]++; }//O(n) // return frequency number int freqQuery(int x, unordered_map<int, int> hm) { return hm[x]; }//O(1) int main() { int a[] = {1, 3, 2, 4, 2, 1}; unordered_map<int, int> hm; //create frequency container countFreq(a, 6, hm); // intialize frequency container cout << freqQuery(2, hm) << endl; // 2 cout << freqQuery(3, hm) << endl; // 1 cout << freqQuery(5, hm) << endl; // 0 }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter