---
title: "ByteByteGo: Pair Sum - Unsorted"
url: https://perrotta.dev/2025/11/bytebytego-pair-sum-unsorted/
last_updated: 2026-02-22
---


[ByteByteGo: Pair Sum — Unsorted](https://bytebytego.com/exercises/coding-patterns/hash-maps-and-sets/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))))
```

