Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem: https://leetcode.com/problems/majority-element/
- // Solution: https://www.youtube.com/watch?v=cLuLy6dy7n8
- class Solution {
- public int majorityElement(int[] nums) {
- if(nums.length == 1) {
- return nums[0];
- }
- HashMap<Integer,Integer> map = new HashMap<Integer, Integer>();
- for(int i: nums) {
- if(map.containsKey(i) && map.get(i) + 1 > nums.length / 2) {
- return i;
- } else {
- map.put(i, map.getOrDefault(i, 0) + 1);
- }
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement