LoginSignup
0
0

More than 1 year has passed since last update.

Leetcode 540. Single Element in a Sorted Array

Posted at

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();
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0