Problem statement

https://leetcode.com/problems/find-smallest-common-element-in-all-rows/

Solution

All we need to do is to calculate frequencies of all elements and take element with frequency m if we have it and -1 if we are not.

Complexity

It is O(mn) both for time and space

Code

class Solution:
    def smallestCommonElement(self, mat):
        cnt = Counter(chain(*mat))
        t = max(cnt.values())
        return -1 if t < len(mat) else min(x for x in cnt if cnt[x] == t)