Definition
A set is a collection of distinct elements: each possible element is either in it or not, and adding an element that is already present changes nothing. A multiset, also called a bag, also records how many times each element occurs, a number called its multiplicity. Neither keeps an order. The difference that matters: a set answers which values occurred, while a multiset also answers how often, so counts, totals and averages over all occurrences need a multiset or a list.
The idea
A program logs how long each of three requests took: 1 millisecond (ms), 1 ms and 9 ms. Someone removes duplicate entries before computing statistics, and the log becomes the two values 1 and 9. But the two requests that took 1 ms were different requests that happened to take the same time. Removing one deletes a real observation, and the average duration moves from ms to ms.
Picture three levels of record keeping. A list remembers every observation and its order. A multiset forgets the order but keeps how often each value occurred. A set also forgets how often. No later computation can recover what a level has forgotten. Deduplicating, meaning removing duplicate entries, turns a multiset into a set. That is correct only when the duplicates are copies of one observation, not separate observations that happen to be equal.
How it works
A set supports membership (is present?), insertion (with no effect if is present), union, intersection and difference (the elements in either set, in both, or in the first only), and size, the number of distinct elements. A hash table §14.7 makes the average cost of a membership test independent of the size; a balanced search tree §14.10 makes it grow with the logarithm of the size and keeps the elements sorted.
A multiset is stored as a map from each element to its multiplicity, a positive whole number; an absent element has multiplicity 0. Inserting adds 1 to its multiplicity, removing one copy subtracts 1, and the size is the sum of the multiplicities. Combining two multisets has several meanings: the sum adds multiplicities, the union takes the larger and the intersection the smaller. Python’s §12.23 collections.Counter is such a map. The C++ container std::multiset instead stores every copy separately, which keeps any data that the comparison ignores.
Database tables are multisets by default. In SQL (Structured Query Language), UNION ALL keeps duplicate rows and UNION removes them; COUNT(*) counts rows and COUNT(DISTINCT x) distinct non-null values.
To deduplicate measurement records correctly:
Give each record an identity, a key that names the event it describes, such as (source device, sequence number), where the source increases the sequence number for every event.
Group records by identity, not by value. Records with equal values but different identities are separate events, and all of them stay.
Within one identity, check that the copies agree. Identical copies, such as a log line written twice after a retried transfer, collapse to one record. Copies that disagree are a conflict to report, not a duplicate to drop.
The records that remain give the denominator, the count that a statistic such as an average divides by. The right denominator follows from the estimand §11.2, the quantity the study sets out to estimate: for the mean duration per request it counts requests; for the share of devices that ever failed it counts devices, and many records from one device count once.
Worked example
A log holds four rows, each giving an event identity and a duration:
e1: 1 ms;e2: 1 ms;e2: 1 ms, the same line written a second time;e3: 9 ms.
The estimand is the mean duration per event, and there are three events.
Keep every row. The total is ms over 4 rows, a mean of ms. Event
e2counts twice, so the denominator 4 is one too large.Deduplicate by identity. The rows for
e1,e2ande3remain. The total is ms, a mean of ms. This is the intended answer.Deduplicate by value. The set remains. The total is ms, a mean of ms. Events
e1ande2merged because their durations were equal, so the denominator 2 is one too small.
As a multiset, the correct sample is and , where is the multiplicity of the value . The number of observations is , and the total is ms, as in method 2. The set keeps only the fact that both multiplicities are positive.
Other statistics move too. The share of observations at or below 1 ms, which is the value at 1 ms of the empirical cumulative distribution function §11.17 (ECDF), is for the correct sample, with every row kept, and for the set. The median §11.13, the middle value, is 1 ms for the correct sample , but ms for the set.
Stated precisely
Let be the set of possible values. A multiset over is a function that gives each in a multiplicity in . Its support is the set of values that occur. The number of observations is , and the number of distinct values is the size of the support. Deduplicating by value replaces every positive by 1, so it changes into . The two are equal exactly when no value occurs more than once.
For numbers, the mean of the multiset is , and the mean after deduplicating by value is . The two means are equal whenever all positive multiplicities are equal; otherwise they are equal only for particular values, by coincidence.
Deduplicating records by identity is correct under one condition: all records with the same identity describe the same event and carry the same values. The result then holds exactly one record per event, so every statistic per event uses the intended denominator.
Limits and common mistakes
Equal values do not mean the same event, and one event can appear with different values. Deduplicating by value merges separate events; deduplicating whole rows keeps two copies of one event that differ only in a field added on the way, such as the time the line was logged. Only an identity key settles which records describe one event.
Deduplicating by a hash of each record, a short value computed from its content, merges two different records whenever their hashes are equal, which is a hash collision §14.8.
Deduplication does not create independence. One device that reports ten events gives ten events but only one device. When the study’s question is about devices, the experimental unit §11.3, the unit that the sample size counts, is the device. Treating the ten events as ten independent observations of devices is pseudoreplication §11.6.
A set needs an equality rule. The texts 1.0 and 1.00 are different strings but the same number, so a set of strings keeps both and a set of numbers keeps one.
Sets and multisets keep no order; when order or timing matters, keep the list.
Questions answered
Does deduplicating records change the intended scientific denominator?
Deduplicating records changes the intended denominator, the count a statistic divides by, whenever it removes a separate observation; removing only extra copies of one observation restores it instead. That denominator counts the units the study is about, such as events, programs or devices. Deduplicating by value treats the sample as a set, which keeps each value once, and drops separate events that happened to be equal: durations of 1, 1 and 9 ms become 1 and 9, the count falls from 3 to 2, and the mean rises from about 3.67 ms to 5 ms. Deduplicating by an identity key, such as source and sequence number, removes only repeated copies of one event. So define the unit and its identity first, deduplicate by identity, keep repeated values, and report copies that share an identity but disagree as conflicts.
Related entries: Estimand and estimator (§11.2), Empirical cumulative distribution function (ECDF) (§11.17), Hash collision versus cryptographic collision (§14.8).