MCPcopy Create free account
hub / github.com/ROUTINE-STUDY/Algorithm / MajorityElement

Class MajorityElement

LeetCode/Array/169. Majority Element/kaki5507.java:6–38  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4import java.util.HashMap;
5
6public class MajorityElement {
7
8 public static void main(String[] args) {
9
10
11 // 첫 번째 방법
12 // 정렬 후, 배열 중간에는 가장 많이나오는 인덱스가 존재.
13 Arrays.sort(nums);
14 return nums[nums.length / 2];
15
16 // 두 번째 방법
17 // map을 이용하여 map밸류가 nums의 길이의 반 보다 크면, 가장 많이나오는 인덱스가 존재.
18 /*
19 HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
20 int count = 0;
21 for(int i : nums){
22 if(map.containsKey(i)){
23 count = map.get(i);
24 map.put(i, count+1);
25 }else{
26 map.put(i , 1);
27 }
28
29 if(map.get(i) > nums.length/2){
30 return i;
31 }
32 }
33
34 return 999;
35 */
36 }
37
38}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected