File tree Expand file tree Collapse file tree 1 file changed +8
-4
lines changed Expand file tree Collapse file tree 1 file changed +8
-4
lines changed Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def majorityElement (self , nums : List [int ]) -> int :
3
- nums_dict = {}
3
+ """
4
+ majorityElement returns the element of array nums that appears more than [n / 2] times
5
+ nums array (n == nums.length) (1 <= n <= 5 * 104) (-109 <= nums[i] <= 109)
6
+ """
7
+ nums_dict = {} #create empty dict
4
8
for number in nums :
5
- nums_dict .setdefault (number ,0 )
6
- nums_dict [number ] += 1
7
- max_number = max (nums_dict , key = lambda x : nums_dict [x ])
9
+ nums_dict .setdefault (number ,0 ) #add number in nums to dict set value to 0
10
+ nums_dict [number ] += 1 #increment value by 1 for each occurrence of nums
11
+ max_number = max (nums_dict , key = lambda x : nums_dict [x ]) #get key with value of highest increment
8
12
return max_number
You can’t perform that action at this time.
0 commit comments