The Flajolet–Martin algorithm is an
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
for approximating the number of distinct elements in a stream with a single pass and space-consumption logarithmic in the maximal number of possible distinct elements in the stream (the
count-distinct problem
In computer science, the count-distinct problem
(also known in applied mathematics as the cardinality estimation problem) is the problem of finding the number of distinct elements in a data stream with repeated elements.
This is a well-known proble ...
). The algorithm was introduced by
Philippe Flajolet and
G. Nigel Martin in their 1984 article "Probabilistic Counting Algorithms for Data Base Applications". Later it has been refined in "LogLog counting of large cardinalities" by
Marianne Durand and
Philippe Flajolet, and "
HyperLogLog: The analysis of a near-optimal cardinality estimation algorithm" by
Philippe Flajolet et al.
In their 2010 article "An optimal algorithm for the distinct elements problem",
Daniel M. Kane,
Jelani Nelson and
David P. Woodruff give an improved algorithm, which uses nearly optimal space and has optimal ''O''(1) update and reporting times.
The algorithm
Assume that we are given a
hash function
A hash function is any Function (mathematics), function that can be used to map data (computing), data of arbitrary size to fixed-size values, though there are some hash functions that support variable-length output. The values returned by a ...
that maps input
to integers in the range