Collection Kit

CountMinSketch

Probabilistic

A CountMinSketch is a probabilistic data structure used for frequency estimation of events in a stream. It provides approximate counts of elements with a small memory footprint.

Import Statement
import { CountMinSketch } from "collection-kit";

Key Features

  • Space Efficiency: Uses a fixed amount of space to estimate frequencies.
  • Probabilistic Results: Can return approximate counts with a known error margin.

Common Operations

Add
Increment the count for a specific element.
Estimate
Retrieve the estimated count for an element.

Example Code

import { CountMinSketch } from "collection-kit";

const cms = new CountMinSketch(100, 5); // width, depth
cms.add("apple");
cms.add("apple");
cms.add("banana");

console.log("Count apple:", cms.estimate("apple"));   // ~2
console.log("Count banana:", cms.estimate("banana")); // ~1