본문 바로가기

IT/알고리즘

[코딜리티] - Lesson 2. OddOccurrencesInArray - 1



안녕하세요 남갯입니다.


lesson 2


안드로이드에 SparseBooleanArray 관련 코드가 있다 


저게 더 효율이 있다고 하니 확인해보도록 하자








import android.util.SparseBooleanArray;

import java.util.HashMap;
import java.util.Map;

public class OddOccurrencesInArray {

/*// 홀수개 찾기// android
public int solution(int[] A) {
// write your code in Java SE 8
SparseBooleanArray sparseArray = new SparseBooleanArray();
for (int idx = 0; idx < A.length; idx++) {
//홀수 true , 짝수 false
sparseArray.append(idx, !sparseArray.get(idx,false));
}
return sparseArray.indexOfValue(true);
}*/

// 홀수 찾기
public int solution(int[] A) {
// write your code in Java SE 8
int a = 0;
Map<Integer, Boolean> hashMap = new HashMap<>();
for (int idx = 0; idx < A.length; idx++) {
//홀수 true , 짝수 false
hashMap.put(A[idx], !hashMap.getOrDefault(A[idx], false));
}
for (Integer result : hashMap.keySet()) {
if (hashMap.get(result)) {
return result;
}
}
return a;
}


}