# Number of list elements less than a given integer

I'm trying to make a function whose input is a list, and that outputs a list for which the nth element is the number of elements in the input less than n. So far, I have

l2 = Table[Length[Select[l1, # < n &]], {n, 1, M}]


Where l2 is the output list, l1 is the input list, and M is the desired length of l2. This gives the correct output, but is there a faster way to do this, as this function can get rather slow for larger lists.

An example of the function:

{2,2,3,3,7} —$$>$$ {0,0,2,4,4}.

• Is your input list always sorted? Sep 22, 2019 at 17:25
• Yes it is always sorted Sep 22, 2019 at 17:36

The BoolEval package can be used here to get a good combination of performance and clean syntax:

<< BoolEval
Table[BoolCount[list < n], {n, 5}]


{0, 0, 2, 4, 4}

list = RandomReal[{0, 100}, 100000];
Accumulate[BinCounts[list, {0, Ceiling[Max[list]], 1}]]

l1 = {2, 2, 3, 3, 7};
M = 4;
l2  = Table[Length[Select[l1, # < n &]], {n, 1, M}]


{0, 0, 2, 4}

### UnitStep + Total

Total[1 - UnitStep[l1 - #] & /@ Range[M], {2}]
(* or Total[1 - UnitStep[l1 - #]] & /@ Range[M] *)


{0, 0, 2, 4}

### Clip + Unitize + Total

Total[1 - Unitize@Clip[l1, {#, ∞}, {0, 1}] & /@ Range[ M], {2}]
(* or Total[1 - Unitize @ Clip[l1, {#, ∞}, {0, 1}]] & /@ Range[M] *)
`

{0, 0, 2, 4}