does anyone know how to count all numbers or characters in list and print it in pair in this format: (number . number_of_occurrences). For example:
(count ‘(3 1 3 2 1 2 3 3 3))
((3 . 5) (1 . 2) (2 . 2))
(count ‘(d b a c b b a))
((d . 1) (b . 3) (a . 2) (c . 1))
Thanks in advance for helping me 🙂
Here’s an idea – use a hash table to keep track of the number of occurrences. This is an
O(n)procedure:Alternatively, here’s a simpler version (it doesn’t use
filter) of @mobyte’s solution in Scheme – noticing that this isO(n^2)and hence less efficient than the hash table-based procedure:Either way, It works as expected: