Skip to content

1346. Check If N and Its Double Exist 👍

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  bool checkIfExist(vector<int>& arr) {
    unordered_set<int> seen;

    for (const int a : arr) {
      if (seen.contains(a * 2) || a % 2 == 0 && seen.contains(a / 2))
        return true;
      seen.insert(a);
    }

    return false;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
  public boolean checkIfExist(int[] arr) {
    Set<Integer> seen = new HashSet<>();

    for (final int a : arr) {
      if (seen.contains(a * 2) || a % 2 == 0 && seen.contains(a / 2))
        return true;
      seen.add(a);
    }

    return false;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
  def checkIfExist(self, arr: list[int]) -> bool:
    seen = set()

    for a in arr:
      if a * 2 in seen or a % 2 == 0 and a // 2 in seen:
        return True
      seen.add(a)

    return False