540. Single Element in a Sorted Array
アプローチ
Hashmap
class Solution {
public int singleNonDuplicate(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
}
HashMap<Integer, Integer> hashMap = new HashMap<>();
for (int num : nums) {
hashMap.put(num, hashMap.getOrDefault(num, -1) + 1);
}
return hashMap.keySet().stream().filter(key-> hashMap.get(key) == 0).findFirst().get();
}
}