Cumulative Counts
Challenge
Given an array of numbers return the cumulative count of each item.
This is the number of times an item has occurred so far.
Examples
[1,1,2,2,2,1,1,1,3,3] -> [1,2,1,2,3,3,4,5,1,2]
[3,7,5,4,9,2,3,2,6,6] -> [1,1,1,1,1,1,2,2,1,2]
Brownie points for beating my 7 byte APL answer.
BQN, 5 bytes ``` 1+⊒ ``` T …
4y ago
[APL (Dyalog Unicode)], 11 byt …
4y ago
[Jelly], 4 bytes ¹Ƥċ" …
4y ago
Japt, 14 11 8 bytes £¯Y …
4y ago
[APL (Dyalog Unicode)], 11 7 b …
4y ago
[Ruby], 36 bytes ```ruby - …
4y ago
[JavaScript (Node.js)], 34 29 …
2y ago
[Husk], 4 bytes Sz#ḣ …
4y ago
Scala 3, 50 44 bytes ```scala …
4y ago
[Jelly], 7 bytes =þ`ÄŒD …
4y ago
[Lean 4], 111 bytes ```lean …
14d ago
Python 3, 50 bytes We can s …
15d ago
Haskell + hgl, 10 bytes ``` …
2y ago
Python 3, 70 bytes ```pytho …
2y ago
Factor, 122 bytes ``` USIN …
2y ago
Vyxal, 4 bytes ``` KƛtO ``` …
2y ago
[Python 3], 74 bytes …
3y ago
[Python 3.8 (pre-release)], 69 …
2y ago
J, 9 bytes ```J 1#.]=&><\ …
3y ago
J, 24 char ```([: +/ [: ( …
3y ago
Ruby, 31 bytes ```ruby ->a …
3y ago
21 answers
Haskell + hgl, 10 bytes
mpn$ce~<gj
Explanation
-
mpn
map across all non-empty prefixes of the input ... -
ce
count the number of elements in each prefix equal to ... -
gj
the last element of that prefix.
Reflection
I'm happy with this. It doesn't take any extra steps, and the glue is cheap.
0 comment threads
- ← Previous
- 1
- 2
- Next →
0 comment threads