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 C# DSA
implementation can be compared directly with the rest of the DSA track.
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.
Basic Implementation
basic.cs
Replay: real traced execution (multi-file project)
using System;
using System.Collections.Generic;
var arr = new[] {3, 5, 2, 5, 3, 8, 2};
var count = new Dictionary<int, int>();
foreach (var value in arr) {
count[value] = count.GetValueOrDefault(value) + 1;
}
foreach (var value in arr) {
if (count[value] == 1) {
Console.WriteLine(value);
break;
}
}
arr ← [3, 5, 2, 5, 3, 8, 2]
1using System;2using System.Collections.Generic;values this step[3, 5, 2, 5, 3, 8, 2]arrcount ← {}
4var arr = new[] {3, 5, 2, 5, 3, 8, 2};5var count = new Dictionary<int, int>();6foreach (var value in arr) {values this step{}countcount ← {3: 1}
4var arr = new[] {3, 5, 2, 5, 3, 8, 2};5var count = new Dictionary<int, int>();6foreach (var value in arr) {values this step{} → {3: 1}count3valuecount ← {3: 1, 5: 1}
4var arr = new[] {3, 5, 2, 5, 3, 8, 2};5var count = new Dictionary<int, int>();6foreach (var value in arr) {values this step{3: 1} → {3: 1, 5: 1}count5valuecount ← {3: 1, 5: 1, 2: 1}
4var arr = new[] {3, 5, 2, 5, 3, 8, 2};5var count = new Dictionary<int, int>();6foreach (var value in arr) {values this step{3: 1, 5: 1} → {3: 1, 5: 1, 2: 1}count2valuecount ← {3: 1, 5: 2, 2: 1}
4var arr = new[] {3, 5, 2, 5, 3, 8, 2};5var count = new Dictionary<int, int>();6foreach (var value in arr) {values this step{3: 1, 5: 1, 2: 1} → {3: 1, 5: 2, 2: 1}count5valuecount ← {3: 2, 5: 2, 2: 1}
4var arr = new[] {3, 5, 2, 5, 3, 8, 2};5var count = new Dictionary<int, int>();6foreach (var value in arr) {values this step{3: 1, 5: 2, 2: 1} → {3: 2, 5: 2, 2: 1}count3valuecount ← {3: 2, 5: 2, 2: 1, 8: 1}
4var arr = new[] {3, 5, 2, 5, 3, 8, 2};5var count = new Dictionary<int, int>();6foreach (var value in arr) {values this step{3: 2, 5: 2, 2: 1} → {3: 2, 5: 2, 2: 1, 8: 1}count8valuecount ← {3: 2, 5: 2, 2: 2, 8: 1}
4var arr = new[] {3, 5, 2, 5, 3, 8, 2};5var count = new Dictionary<int, int>();6foreach (var value in arr) {values this step{3: 2, 5: 2, 2: 1, 8: 1} → {3: 2, 5: 2, 2: 2, 8: 1}count2valuei ← 0, value ← 3, count[value] ← 2, found ← no
9foreach (var value in arr) {10 if (count[value] == 1) {11 Console.WriteLine(value);values this step0i3value2count[value]nofound{3: 2, 5: 2, 2: 2, 8: 1}counti ← 1, value ← 5, count[value] ← 2, found ← no
9foreach (var value in arr) {10 if (count[value] == 1) {11 Console.WriteLine(value);values this step1i5value2count[value]nofound{3: 2, 5: 2, 2: 2, 8: 1}counti ← 2, value ← 2, count[value] ← 2, found ← no
9foreach (var value in arr) {10 if (count[value] == 1) {11 Console.WriteLine(value);values this step2i2value2count[value]nofound{3: 2, 5: 2, 2: 2, 8: 1}counti ← 3, value ← 5, count[value] ← 2, found ← no
9foreach (var value in arr) {10 if (count[value] == 1) {11 Console.WriteLine(value);values this step3i5value2count[value]nofound{3: 2, 5: 2, 2: 2, 8: 1}counti ← 4, value ← 3, count[value] ← 2, found ← no
9foreach (var value in arr) {10 if (count[value] == 1) {11 Console.WriteLine(value);values this step4i3value2count[value]nofound{3: 2, 5: 2, 2: 2, 8: 1}counti ← 5, value ← 8, count[value] ← 1, found ← yes, result ← 8
9foreach (var value in arr) {10 if (count[value] == 1) {11 Console.WriteLine(value);values this step5i8value1count[value]yesfound8result{3: 2, 5: 2, 2: 2, 8: 1}countstdout ← 8
10if (count[value] == 1) {11 Console.WriteLine(value);12 break;values this step8stdout8result
Complexity
- Time: O(n) average
- Space: O(k) for k distinct values
Implementation notes
- Keep output formatting deterministic. Do not rely on unordered hash-map printing when the lesson needs cross-language comparison.
Dictionary<int, int>stores frequency counts in managed backing storage that grows as needed and is reclaimed by GC.GetValueOrDefault(value) + 1makes missing-key initialization explicit while integer hashing gives average O(1) lookup/update; collisions are handled inside the dictionary.- The second pass reads the managed
int[]in original order, so the replay can show the first key whose final count is one.