229 Majority Element II
Given an integer array of size n
, find all elements that appear more than ⌊ n/3 ⌋
times.
Example 1:
Example 2:
Example 3:
Solution:
class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> result = new ArrayList<Integer>();
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++){
map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
}
int target = nums.length/3;
for (Map.Entry<Integer, Integer> entry : map.entrySet()){
if (entry.getValue() > target){
result.add(entry.getKey());
}
}
return result;
}
}
// TC: O(n)
// SC: O(n)