You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
Input: nums = [2,2,1,1,1,2,2]
Output: 2
### code:- class Solution: def majorityElement(self, nums: List[int]) -> int: dict={} n = len(nums)/2 for ele in nums: try: dict[ele] += 1 except: dict[ele] = 1 for item in dict: if dict[item] >= n: return item
The text was updated successfully, but these errors were encountered:
Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
Input: nums = [2,2,1,1,1,2,2]
Output: 2
### code:-
class Solution: def majorityElement(self, nums: List[int]) -> int: dict={} n = len(nums)/2 for ele in nums: try: dict[ele] += 1 except: dict[ele] = 1 for item in dict: if dict[item] >= n: return item
The text was updated successfully, but these errors were encountered: