thiagowfx's avatar

¬ just serendipity 🍀 (not just serendipity)

LeetCode #1346: Check If N and Its Double Exist

• 91 words • 1 min • updated

LeetCode #1346: Check If N and Its Double Exist:

python
from collections import Counter

class Solution:
    def checkIfExist(self, arr: List[int]) -> bool:
        seen = Counter(arr)
        for num in arr:
            if num == 0:
                if seen[0] > 1:
                    return True
            elif 2 * num in seen.keys():
                return True
        return False

Simpler:

python
class Solution:
    def checkIfExist(self, arr: List[int]) -> bool:
        seen = set()
        for num in arr:
            if 2 * num in seen:
                return True
            if num % 2 == 0 and num // 2 in seen:
                return True

            seen.add(num)

        return False