Quick and dirty O(n) solution

  • 0
    class Solution:
        # @param num, a list of integers
        # @return an integer
        def majorityElement(self, num):
            histo = {}
            for i in num:
                if i in histo:
                    histo[i] += 1
                    histo[i] = 1
            max = -1
            max_i = -1
            for i in num:
                if histo[i] > max:
                    max = histo[i]
                    max_i = i
            return max_i

    I know it doesn't look pythony, and it could be written very elegantly, but I just wanted to write it the quickest possible way.

Log in to reply

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