Build buckets keyed by a shared field, preserving the first-seen key order.

Algorithm

Canonical pairs (a,1), (b,2), (a,3), (c,4), (b,5) print {a: [1, 3], b: [2, 5], c: [4]}. 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.

bucket map Each key owns a list. A new key creates a bucket; a repeated key appends to the existing bucket.

Basic Implementation

basic.scala
Replay: real traced execution (multi-file project)
import scala.collection.mutable.{HashMap, ArrayBuffer}
object Main {
  def main(args: Array[String]): Unit = {
    val pairs = List(("a", 1), ("b", 2), ("a", 3), ("c", 4), ("b", 5))
    val groups = HashMap.empty[String, ArrayBuffer[Int]]
    val order = ArrayBuffer.empty[String]
    for ((key, value) <- pairs) {
      if (!groups.contains(key)) {
        groups(key) = ArrayBuffer.empty[Int]
        order += key
      }
      groups(key) += value
    }
    val parts = order.map(key => s"$key: [${groups(key).mkString(", ")}]")
    println("{" + parts.mkString(", ") + "}")
  }
}
  1. pairs ← [(a, 1), (b, 2), (a, 3), (c, 4), (b, 5)]

    1import scala.collection.mutable.{HashMap, ArrayBuffer}2object Main {
    values this step[(a, 1), (b, 2), (a, 3), (c, 4), (b, 5)]pairs
  2. groups ← {}

    4val pairs = List(("a", 1), ("b", 2), ("a", 3), ("c", 4), ("b", 5))5val groups = HashMap.empty[String, ArrayBuffer[Int]]6val order = ArrayBuffer.empty[String]
    values this step{}groups
  3. groups ← {a: [1]}

    4val pairs = List(("a", 1), ("b", 2), ("a", 3), ("c", 4), ("b", 5))5val groups = HashMap.empty[String, ArrayBuffer[Int]]6val order = ArrayBuffer.empty[String]
    values this step{} {a: [1]}groupsakey1value
  4. groups ← {a: [1], b: [2]}

    4val pairs = List(("a", 1), ("b", 2), ("a", 3), ("c", 4), ("b", 5))5val groups = HashMap.empty[String, ArrayBuffer[Int]]6val order = ArrayBuffer.empty[String]
    values this step{a: [1]} {a: [1], b: [2]}groupsbkey2value
  5. groups ← {a: [1, 3], b: [2]}

    4val pairs = List(("a", 1), ("b", 2), ("a", 3), ("c", 4), ("b", 5))5val groups = HashMap.empty[String, ArrayBuffer[Int]]6val order = ArrayBuffer.empty[String]
    values this step{a: [1], b: [2]} {a: [1, 3], b: [2]}groupsakey3value
  6. groups ← {a: [1, 3], b: [2], c: [4]}

    4val pairs = List(("a", 1), ("b", 2), ("a", 3), ("c", 4), ("b", 5))5val groups = HashMap.empty[String, ArrayBuffer[Int]]6val order = ArrayBuffer.empty[String]
    values this step{a: [1, 3], b: [2]} {a: [1, 3], b: [2], c: [4]}groupsckey4value
  7. groups ← {a: [1, 3], b: [2, 5], c: [4]}

    4val pairs = List(("a", 1), ("b", 2), ("a", 3), ("c", 4), ("b", 5))5val groups = HashMap.empty[String, ArrayBuffer[Int]]6val order = ArrayBuffer.empty[String]
    values this step{a: [1, 3], b: [2], c: [4]} {a: [1, 3], b: [2, 5], c: [4]}groupsbkey5value
  8. stdout ← {a: [1, 3], b: [2, 5], c: [4]}

    14  val parts = order.map(key => s"$key: [${groups(key).mkString(", ")}]")15  println("{" + parts.mkString(", ") + "}")16}
    values this step{a: [1, 3], b: [2, 5], c: [4]}stdout{a: [1, 3], b: [2, 5], c: [4]}groups
  9. bucket 1 after collision ← c -> a, degradation risk ← long chains can degrade lookup toward O(n)

    14  val parts = order.map(key => s"$key: [${groups(key).mkString(", ")}]")15  println("{" + parts.mkString(", ") + "}")16}
    values this stepa c -> abucket 1 after collisionlong chains can degrade lookup toward O(n)degradation riskresize or rehash when load factor growsmitigationcnew key

Complexity

  • Time: O(n) average
  • Space: O(k + n) for buckets and values

Implementation notes

  • Keep output formatting deterministic. Do not rely on unordered hash-map printing when the lesson needs cross-language comparison.
  • The trace highlights the hash table state after each write and includes a collision contrast where one bucket chain grows, showing why long chains can degrade lookup and why real tables resize or rehash.