0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Leetcode 1346. Check If N and Its Double Exist

Posted at

1346. Check If N and Its Double Exist

アプローチ

HashMap

class Solution {
    public boolean checkIfExist(int[] arr) {
        HashMap<Integer, Integer> hashMap = new HashMap<>();

        for (int a : arr) {
            hashMap.put(a, hashMap.getOrDefault(a, 0) + 1);
        }

        for (int a : arr) {
            if (hashMap.containsKey(a * 2)) {

                if (a == 0 && hashMap.get(a) < 2) {
                    continue;
                }
                return true;
            }
        }
        return false;
    }
}
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?