Check If N and Its Double Exist

Problem Id: 1346 Difficulty: Easy Tag: Array


Intuition

Just use a hash set to solve this problem. This would use time complexity O(n) and space complexity O(n).

Pay attaintion to 0, because it is the same with its double.

Solution


class Solution:
    def checkIfExist(self, arr: List[int]) -> bool:
        tmp = set(arr)
        if 0 in tmp and arr.count(0) > 1:
            return True
        for num in arr:
            if num != 0 and 2 * num in tmp:
                return True
        return False