function countDistinctCVM(a, thresh) {
let p = 1;
const X = new Set();
for (let item of a) {
X.delete(item);
if (Math.random() < p) X.add(item);
if (X.size === thresh) {
for (let item of X) if (Math.random() < 0.5) X.delete(item);
p /= 2;
}
}
return X.size / p;
}