// Java program to answer
// queries for frequencies
// in O(1) time.
import java.io.*;
import java.util.*;
class GFG {
static void countFreq(int a[], int n)
{
// Insert elements and their
// frequencies in hash map.
for (int i=0; i<n; i++)
if (hm.containsKey(a[i]) )
hm.put(a[i], hm.get(a[i]) + 1);
else hm.put(a[i] , 1);
}
// Return frequency of x (Assumes that
// countFreq() is called before)
static int query(int x)
{
if (hm.containsKey(x))
return hm.get(x);
return 0;
}
// Driver program
public static void main
(String[] args
) { int a[] = {1, 3, 2, 4, 2, 1};
int n = a.length;
countFreq(a, n);
}
}
Ly8gSmF2YSBwcm9ncmFtIHRvIGFuc3dlcgovLyBxdWVyaWVzIGZvciBmcmVxdWVuY2llcwovLyBpbiBPKDEpIHRpbWUuCiAKaW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEudXRpbC4qOwogCmNsYXNzIEdGRyB7CiAKc3RhdGljIEhhc2hNYXAgPEludGVnZXIsIEludGVnZXI+IGhtID0gbmV3IEhhc2hNYXA8SW50ZWdlciwgSW50ZWdlcj4oKTsKIApzdGF0aWMgdm9pZCBjb3VudEZyZXEoaW50IGFbXSwgaW50IG4pCnsKCQkvLyBJbnNlcnQgZWxlbWVudHMgYW5kIHRoZWlyCgkJLy8gZnJlcXVlbmNpZXMgaW4gaGFzaCBtYXAuCgkJZm9yIChpbnQgaT0wOyBpPG47IGkrKykKCQkJaWYgKGhtLmNvbnRhaW5zS2V5KGFbaV0pICkKCQkJCWhtLnB1dChhW2ldLCBobS5nZXQoYVtpXSkgKyAxKTsKCQkJZWxzZSBobS5wdXQoYVtpXSAsIDEpOwoJfQogCgkvLyBSZXR1cm4gZnJlcXVlbmN5IG9mIHggKEFzc3VtZXMgdGhhdAoJLy8gY291bnRGcmVxKCkgaXMgY2FsbGVkIGJlZm9yZSkKCXN0YXRpYyBpbnQgcXVlcnkoaW50IHgpCgl7CgkJaWYgKGhtLmNvbnRhaW5zS2V5KHgpKQoJCQlyZXR1cm4gaG0uZ2V0KHgpOwoJCX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