Python solution in 3 lines

  • -1
    def majorityElement(self, nums):
        from collections import Counter
        n = Counter(nums)
        return n.most_common(1)[0][0]

    Admittedly, this looks like a cheat. Not entirely sure what the run-time is for the most_common method. Anyone can chime in on this?

  • 1

    Yes, applying most_common method is a cheat :) The interest is to write most_common method urself.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.