Search code examples
pythonlistfrequency

Finding the most common element in a list of lists


I am given a list of lists like this:

pairs = [[(0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (3, 2), (3, 1), (2, 1), (3, 1), (3, 2), (3, 3), (3, 2), (2, 2)], 
         [(2, 2), (2, 1)], 
         [(1, 1), (1, 2), (2, 2), (2, 1)]]

and the desired output is: {(2,2)}.

I need to find the most frequent element(s). It has to return more than one value if there are elements that are repeated just as many times.

I tried solving it with an intersection of three lists, but it prints out {(2,1), (2,2)}, instead of {(2,2)}, since the element (2,2) is repeated two times in the first list.

I saw a few examples with import collections, but I don't understand them so I don't know how to change the code to be suitable for my problem.

I also tried the following:

seen = set()
repeated = set()
for l in pairs:
    for i in set(l):
        if i in seen:
            repeated.add(i)
        if i in repeated:
            repeated.add(i)
        else:
            seen.add(i)

but still doesn't return the correct answer.


Solution

  • Alternative solution without using the Counter method from collections:

    def get_freq_tuple(data):
        counts = {}
        for pairs in data:
            for pair in pairs:
                counts[pair] = counts.get(pair, 0) + 1
    
        return [pair for pair in counts if counts[pair] == max(counts.values())]
    
    if __name__ == "__main__":
        pairs = [[(0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (3, 2), (3, 1), (2, 1),
                  (3, 1), (3, 2), (3, 3), (3, 2), (2, 2)],
                 [(2, 2), (2, 1)],
                 [(1, 1), (1, 2), (2, 2), (2, 1)]]
        print(get_freq_tuple(pairs))
    

    Output:

    [(2, 2)]
    

    Explanation:

    • Count the occurrences of each tuple and store them in a dictionary. The key of the dictionary is the tuple and the value is the occurrence.
    • Filter the tuples in the dictionary by maximum occurrences of the tuples.

    Disclaimer:

    • Using Counter method from collections is much more efficient.

    References: