Skip to content

Commit 7bda7f1

Browse files
majority element
1 parent 086f35e commit 7bda7f1

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

‎Common/src/utils/CommonUtils.java

+13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ public class CommonUtils {
1111
private static final int DEFAULT_TREE_SIZE = 10;
1212
private static final int DEFAULT_UPPER_BOUND = 100;
1313

14+
//How to make a method generic: declare <T> in its method signature
15+
public static <T> void printArray_generic_type(T[] nums) {
16+
for(T i : nums){
17+
System.out.print(i + ", ");
18+
}
19+
System.out.println();
20+
}
21+
22+
public static void main(String...strings){
23+
Integer[] nums = new Integer[]{1,2,3,4,5};
24+
printArray_generic_type(nums);
25+
}
26+
1427
public static void printArray(int[] nums) {
1528
for(int i : nums){
1629
System.out.print(i + ", ");

‎EASY/src/easy/MajorityElement.java

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package easy;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import utils.CommonUtils;
8+
9+
/**169. Majority Element QuestionEditorial Solution My Submissions
10+
Total Accepted: 132691
11+
Total Submissions: 309653
12+
Difficulty: Easy
13+
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
14+
15+
You may assume that the array is non-empty and the majority element always exist in the array.
16+
17+
*/
18+
public class MajorityElement {
19+
20+
public int majorityElement_bit_manipulation(int[] nums){
21+
int[] bit = new int[32];//because an integer is 32 bits, so we use an array of 32 long
22+
for(int num : nums){
23+
for(int i = 0; i < 32; i++){
24+
if((num >> (31-i) & 1) == 1) bit[i]++;//this is to compute each number's ones frequency
25+
}
26+
}
27+
int res = 0;
28+
//this below for loop is to construct the majority element: since every bit of this element would have appeared more than n/2 times
29+
for(int i = 0; i < 32; i++){
30+
bit[i] = bit[i] > nums.length/2 ? 1 : 0;//we get rid of those that bits that are not part of the majority number
31+
res += bit[i]*(1 << (31-i));
32+
}
33+
return res;
34+
}
35+
36+
//saw a really clever solution on Discuss, though it didn't use bit manipulatoin
37+
//this is actually applying a famous algorithm called Moore Voting algorithm: http://www.cs.utexas.edu/~moore/best-ideas/mjrty/example.html
38+
public int majorityElement_moore_voting_algorithm(int[] nums){
39+
int count = 1, majority = nums[0];
40+
for(int i = 1; i < nums.length; i++){
41+
if(count == 0){
42+
count++;
43+
majority = nums[i];
44+
} else if(nums[i] == majority){
45+
count++;
46+
} else count--;
47+
}
48+
return majority;
49+
}
50+
51+
public static void main(String...strings){
52+
int[] nums = new int[]{1,2,3,4,2,3,2,2,4,2};
53+
MajorityElement test = new MajorityElement();
54+
System.out.println(test.majorityElement_bit_manipulation(nums));
55+
}
56+
57+
//my natural idea is to either compute the frequency of each unique number or sort it and return the median, I can hardly think of
58+
//how bit manipulation could come into play for this question
59+
//this is O(n) time.
60+
public int majorityElement_compute_frequency(int[] nums) {
61+
Map<Integer, Integer> map = new HashMap();
62+
for(int i : nums){
63+
map.put(i, map.getOrDefault(i, 0) + 1);
64+
if(map.get(i) > nums.length/2) return i;
65+
}
66+
return -1;
67+
}
68+
69+
//This is O(nlogn) time.
70+
public int majorityElement_sort(int[] nums) {
71+
Arrays.sort(nums);
72+
return nums[nums.length/2];
73+
}
74+
}

0 commit comments

Comments
 (0)