thiagowfx's avatar

Β¬ just serendipity πŸ€ (not just serendipity)

ByteByteGo: Pair Sum - Unsorted

β€’ 102 words β€’ 1 min β€’ updated

ByteByteGo: Pair Sum β€” Unsorted:

python
from typing import List

def pair_sum_unsorted(nums: List[int], target: int) -> List[int]:
    # Option 1: sort it and do two pointers.
    # Option 2: hash map.
    d = {}
    for i, num in enumerate(nums):
        d[num] = i

    for i, num in enumerate(nums):
        if (target - num) in d.keys():
            j = d[target - num]
            if i != j:
                return [i, j]

    return []

Be careful not to pick the same index twice.

Simpler way to construct the hash map: a dict comprehension:

python
d = {num: i for i, num in enumerate(nums)}

Fancier:

python
d = dict(zip(nums, range(len(nums))))