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 Scala 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.scala
Replay: real traced execution (multi-file project)
import scala.collection.mutable.HashMap
object Main {
def main(args: Array[String]): Unit = {
val arr = List(3, 5, 2, 5, 3, 8, 2)
val count = HashMap.empty[Int, Int]
for (value <- arr) {
count(value) = count.getOrElse(value, 0) + 1
}
for (value <- arr) {
if (count(value) == 1) {
println(value)
return
}
}
}
}
arr ← [3, 5, 2, 5, 3, 8, 2]
1import scala.collection.mutable.HashMap2object Main {values this step[3, 5, 2, 5, 3, 8, 2]arrcount ← {}
4val arr = List(3, 5, 2, 5, 3, 8, 2)5val count = HashMap.empty[Int, Int]6for (value <- arr) {values this step{}countcount ← {3: 1}
4val arr = List(3, 5, 2, 5, 3, 8, 2)5val count = HashMap.empty[Int, Int]6for (value <- arr) {values this step{} → {3: 1}count3valuecount ← {3: 1, 5: 1}
4val arr = List(3, 5, 2, 5, 3, 8, 2)5val count = HashMap.empty[Int, Int]6for (value <- arr) {values this step{3: 1} → {3: 1, 5: 1}count5valuecount ← {3: 1, 5: 1, 2: 1}
4val arr = List(3, 5, 2, 5, 3, 8, 2)5val count = HashMap.empty[Int, Int]6for (value <- arr) {values this step{3: 1, 5: 1} → {3: 1, 5: 1, 2: 1}count2valuecount ← {3: 1, 5: 2, 2: 1}
4val arr = List(3, 5, 2, 5, 3, 8, 2)5val count = HashMap.empty[Int, Int]6for (value <- arr) {values this step{3: 1, 5: 1, 2: 1} → {3: 1, 5: 2, 2: 1}count5valuecount ← {3: 2, 5: 2, 2: 1}
4val arr = List(3, 5, 2, 5, 3, 8, 2)5val count = HashMap.empty[Int, Int]6for (value <- 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}
4val arr = List(3, 5, 2, 5, 3, 8, 2)5val count = HashMap.empty[Int, Int]6for (value <- 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}
4val arr = List(3, 5, 2, 5, 3, 8, 2)5val count = HashMap.empty[Int, Int]6for (value <- 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
1import scala.collection.mutable.HashMap2object Main {values this step0i3value2count[value]nofound{3: 2, 5: 2, 2: 2, 8: 1}counti ← 1, value ← 5, count[value] ← 2, found ← no
1import scala.collection.mutable.HashMap2object Main {values this step1i5value2count[value]nofound{3: 2, 5: 2, 2: 2, 8: 1}counti ← 2, value ← 2, count[value] ← 2, found ← no
1import scala.collection.mutable.HashMap2object Main {values this step2i2value2count[value]nofound{3: 2, 5: 2, 2: 2, 8: 1}counti ← 3, value ← 5, count[value] ← 2, found ← no
1import scala.collection.mutable.HashMap2object Main {values this step3i5value2count[value]nofound{3: 2, 5: 2, 2: 2, 8: 1}counti ← 4, value ← 3, count[value] ← 2, found ← no
1import scala.collection.mutable.HashMap2object Main {values this step4i3value2count[value]nofound{3: 2, 5: 2, 2: 2, 8: 1}counti ← 5, value ← 8, count[value] ← 1, found ← yes, result ← 8
1import scala.collection.mutable.HashMap2object Main {values this step5i8value1count[value]yesfound8result{3: 2, 5: 2, 2: 2, 8: 1}countstdout ← 8
10if (count(value) == 1) {11 println(value)12 returnvalues this step8stdout8result
Complexity
- Time: O(n) average
- Space: O(k) for k distinct values
Implementation notes
- This checked source uses integer input, not string or character iteration:
val arr = List(3, 5, 2, 5, 3, 8, 2). val count = HashMap.empty[Int, Int]binds a mutable ScalaHashMap; the binding isval, but the table contents are updated in place.- The first pass runs
for (value <- arr)in list order and writescount(value) = count.getOrElse(value, 0) + 1. getOrElse(value, 0)supplies the default count for a missing key before the increment.- The trace records logical table states only; it does not expose bucket order, resizing, or collision behavior.
- After counting, the table is
{3: 2, 5: 2, 2: 2, 8: 1}. - The second pass scans the original list again. Values
3,5,2,5, and3have frequency2; value8has frequency1. - On the first unique value, the code calls
println(value)andreturn, so the exact output is8. If no value matched, this source would fall through without printing a sentinel.