Commonest¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Commonest[list] gives a list of the elements that are the most common in list.
Commonest[list, n] gives a list of the n most common elements in list.
Examples¶
All examples below are verified against the current Mathilda build.
In[1]:= Commonest[{b, a, c, 2, a, b, 1, 2}]
Out[1]= {b, a, 2}
In[2]:= Commonest[{b, a, c, 2, a, b, 1, 2}, 4]
Out[2]= {b, a, c, 2}
In[3]:= Commonest[{b, a, c, 2, a, b, 1, 2}, UpTo[6]]
Out[3]= {b, a, c, 2, 1}
In[4]:= Commonest[{1, 2, 2, 3, 3, 3, 4}]
Out[4]= {3}
In[5]:= Commonest[{a, E, Sin[y], E, a, 7}]
Out[5]= {a, E}
Implementation notes¶
Algorithm. builtin_commonest (in src/list.c) tallies element multiplicities with a HashTable (one pass, O(N)), recording each distinct element's count and first-appearance index. The tallies are packed into CommonestItem records and sorted by descending count (ties broken by first occurrence) to find the maximum multiplicity. Without a count argument it returns every element sharing the maximum count; Commonest[list, n] / Commonest[list, UpTo[n]] returns up to the n most frequent.
Data structures. Parallel Expr** / int64_t* arrays for unique elements and their multiplicities, a HashTable mapping element → index, and a CommonestItem array (element, count, first_index) used for the stable sort.
Protected.- When several elements occur with equal frequency,
Commonestpicks first the ones that occur first inlist. Commonest[list, n]returns thencommonest elements in the order they appear inlist.Commonest[list, UpTo[n]]returns thencommonest elements, or as many as are available.- A message
Commonest::dstlmsis generated if there are fewer distinct elements than requested by an integern.
Attributes: Protected.
Implementation status¶
Stable — documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
References¶
- Source:
src/list.c - Specification:
docs/spec/builtins/structural-manipulation.md
Notes & additional examples¶
Worked examples¶
Notes¶
Commonest[list] returns every element tied for the highest frequency; Commonest[list, n] returns the n most common. The quadratic-residue example above shows the three nonzero residues mod 7 each occurring equally often.