#1346
Easy Algorithms Check if n and its double exist
Array Hash Table Two Pointers Binary Search Sorting
41.7% acceptance
Feb 25, 2026
2515
257
Given an array arr of integers, check if there exist two indices i and j such that :
i != j
0 <= i, j < arr.length
arr[i] == 2 * arr[j]
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn check_if_exist(arr: Vec<i32>) -> bool {
let mut set = std::collections::HashSet::new();
for &x in &arr {
if set.contains(&(x * 2)) || (x % 2 == 0 && set.contains(&(x / 2))) {
return true;
}
set.insert(x);
}
false
}
}