Skip to content

Commit 05e15bb

Browse files
Kevin Naughton JrKevin Naughton Jr
authored andcommitted
add adobe directory
1 parent 504af1b commit 05e15bb

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

company/adobe/MajorityElement.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
2+
//You may assume that the array is non-empty and the majority element always exist in the array.
3+
4+
class MajorityElement {
5+
public int majorityElement(int[] nums) {
6+
if(nums.length == 1) {
7+
return nums[0];
8+
}
9+
10+
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
11+
for(int current: nums) {
12+
if(map.containsKey(current) && map.get(current) + 1 > nums.length / 2) {
13+
return current;
14+
} else if(map.containsKey(current)) {
15+
map.put(current, map.get(current) + 1);
16+
} else {
17+
map.put(current, 1);
18+
}
19+
}
20+
21+
//no majority element exists
22+
return -1;
23+
}
24+
}

leetcode/array/MajorityElement.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
2+
//You may assume that the array is non-empty and the majority element always exist in the array.
3+
4+
class MajorityElement {
5+
public int majorityElement(int[] nums) {
6+
if(nums.length == 1) {
7+
return nums[0];
8+
}
9+
10+
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
11+
for(int current: nums) {
12+
if(map.containsKey(current) && map.get(current) + 1 > nums.length / 2) {
13+
return current;
14+
} else if(map.containsKey(current)) {
15+
map.put(current, map.get(current) + 1);
16+
} else {
17+
map.put(current, 1);
18+
}
19+
}
20+
21+
//no majority element exists
22+
return -1;
23+
}
24+
}

0 commit comments

Comments
 (0)