Hash Tables
First Non-Repeating Value
Find the first input value whose final frequency is one.
Algorithm
Canonical input [3, 5, 2, 5, 3, 8, 2] prints 8.
The replay uses the same input in every language, so this Python DSA
implementation can be compared directly with the rest of the DSA track.
Basic Implementation
basic.py
arr = [3, 5, 2, 5, 3, 8, 2]
count = {}
for value in arr:
count[value] = count.get(value, 0) + 1
for value in arr:
if count[value] == 1:
print(value)
break
Complexity
- Time: O(n) average
- Space: O(k) for k distinct values
Implementation notes
- Python uses a plain
dictnamedcount, with immutableintvalues as keys and integer frequencies as values.count.get(value, 0) + 1performs a hash lookup with a default, then the assignment inserts or updates that entry. - The implementation is intentionally two pass: the first loop builds the full
count table, and the second loop walks
arragain so original list order, not dict insertion order, decides the first non-repeating value. - Hash collisions and table resizing are not visible for this small fixture;
Python manages
countwhile it is referenced and can reclaim it once unreferenced. The replay shows each count update, then scans untilcount[8] == 1and prints8.
two-pass lookup
The first pass builds a frequency table. The second pass keeps the original order and stops at the first value with frequency one.