Example :
Input: [1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2, 2, 2] Output: 1: 5 2: 4 3: 3 4: 3 5: 2 Explanation: Here 1 occurs 5 times, 2 occurs 4 times and so on ...
The problem may be solved in different ways. A simple approach is to iterate over the list and use every single element of the list as the key of the dictionary, and store the corresponding counter of that key as values. Below is the Python code for this approach:
Exit :
1: 5 2: 4 3: 3 4: 3 5: 2
Time complexity: O (N), where N — the length of the list.
Alternative way: An alternative approach would be to use the list.count () method. This makes the program much more compact without affecting the runtime. Below is the Python code for this:


Exit:
1: 5 2: 4 3: 3 4: 3 5: 2
Time complexity: O (N), where N — list length.
Related article:
Counting the frequencies of all elements in an array in Python using the collections module