Since every row in our result array can have only distinct elements, we need a data structure that is known for holding distinct values - drumrolls - a set or a hashtable. Let’s use a hashtable since we need the frequencies of each elements as well.
The number of rows in the result array would be the maximum frequency of an element.
Code:
Python3
Big O Analysis
-
Runtime
The runtime complexity here is since all operations we are doing are linear time -
Counter()
,max()
,list()
. -
Memory
The memory usage is since we use the
collections.Counter
object to store frequencies.
— A