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 Go 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.go
Replay: real traced execution (multi-file project)
package main

import "fmt"

func main() {
    arr := []int{3, 5, 2, 5, 3, 8, 2}
    count := map[int]int{}
    for _, value := range arr {
        count[value]++
    }
    for _, value := range arr {
        if count[value] == 1 {
            fmt.Println(value)
            break
        }
    }
}
  1. arr ← [3, 5, 2, 5, 3, 8, 2]

    1package main
    values this step[3, 5, 2, 5, 3, 8, 2]arr
  2. count ← {}

    6arr := []int{3, 5, 2, 5, 3, 8, 2}7count := map[int]int{}8for _, value := range arr {
    values this step{}count
  3. count ← {3: 1}

    6arr := []int{3, 5, 2, 5, 3, 8, 2}7count := map[int]int{}8for _, value := range arr {
    values this step{} {3: 1}count3value
  4. count ← {3: 1, 5: 1}

    6arr := []int{3, 5, 2, 5, 3, 8, 2}7count := map[int]int{}8for _, value := range arr {
    values this step{3: 1} {3: 1, 5: 1}count5value
  5. count ← {3: 1, 5: 1, 2: 1}

    6arr := []int{3, 5, 2, 5, 3, 8, 2}7count := map[int]int{}8for _, value := range arr {
    values this step{3: 1, 5: 1} {3: 1, 5: 1, 2: 1}count2value
  6. count ← {3: 1, 5: 2, 2: 1}

    6arr := []int{3, 5, 2, 5, 3, 8, 2}7count := map[int]int{}8for _, value := range arr {
    values this step{3: 1, 5: 1, 2: 1} {3: 1, 5: 2, 2: 1}count5value
  7. count ← {3: 2, 5: 2, 2: 1}

    6arr := []int{3, 5, 2, 5, 3, 8, 2}7count := map[int]int{}8for _, value := range arr {
    values this step{3: 1, 5: 2, 2: 1} {3: 2, 5: 2, 2: 1}count3value
  8. count ← {3: 2, 5: 2, 2: 1, 8: 1}

    6arr := []int{3, 5, 2, 5, 3, 8, 2}7count := map[int]int{}8for _, value := range arr {
    values this step{3: 2, 5: 2, 2: 1} {3: 2, 5: 2, 2: 1, 8: 1}count8value
  9. count ← {3: 2, 5: 2, 2: 2, 8: 1}

    6arr := []int{3, 5, 2, 5, 3, 8, 2}7count := map[int]int{}8for _, value := range arr {
    values this step{3: 2, 5: 2, 2: 1, 8: 1} {3: 2, 5: 2, 2: 2, 8: 1}count2value
  10. i ← 0, value ← 3, count[value] ← 2, found ← no

    11for _, value := range arr {12    if count[value] == 1 {13        fmt.Println(value)
    values this step0i3value2count[value]nofound{3: 2, 5: 2, 2: 2, 8: 1}count
  11. i ← 1, value ← 5, count[value] ← 2, found ← no

    11for _, value := range arr {12    if count[value] == 1 {13        fmt.Println(value)
    values this step1i5value2count[value]nofound{3: 2, 5: 2, 2: 2, 8: 1}count
  12. i ← 2, value ← 2, count[value] ← 2, found ← no

    11for _, value := range arr {12    if count[value] == 1 {13        fmt.Println(value)
    values this step2i2value2count[value]nofound{3: 2, 5: 2, 2: 2, 8: 1}count
  13. i ← 3, value ← 5, count[value] ← 2, found ← no

    11for _, value := range arr {12    if count[value] == 1 {13        fmt.Println(value)
    values this step3i5value2count[value]nofound{3: 2, 5: 2, 2: 2, 8: 1}count
  14. i ← 4, value ← 3, count[value] ← 2, found ← no

    11for _, value := range arr {12    if count[value] == 1 {13        fmt.Println(value)
    values this step4i3value2count[value]nofound{3: 2, 5: 2, 2: 2, 8: 1}count
  15. i ← 5, value ← 8, count[value] ← 1, found ← yes, result ← 8

    11for _, value := range arr {12    if count[value] == 1 {13        fmt.Println(value)
    values this step5i8value1count[value]yesfound8result{3: 2, 5: 2, 2: 2, 8: 1}count
  16. stdout ← 8

    1package main
    values this step8stdout8result

Complexity

  • Time: O(n) average
  • Space: O(k) for k distinct values

Implementation notes

  • Go stores the input as arr := []int{3, 5, 2, 5, 3, 8, 2} and counts with count := map[int]int{}; there is no byte, rune, or string iteration in this checked source.
  • The first for _, value := range arr copies each int into value and uses count[value]++. A missing key reads as the int zero value, so the first increment creates a count of 1.
  • The trace records the count table growing through {3: 1}, {3: 1, 5: 1}, {3: 1, 5: 1, 2: 1}, then updating repeated values to the final {3: 2, 5: 2, 2: 2, 8: 1}.
  • The second pass ranges over arr again, not over the map, so the scan order is deterministic despite Go's unspecified map iteration order.
  • That scan skips indices 0 through 4 because values 3, 5, and 2 have frequency 2; at index 5, count[8] == 1, so fmt.Println(value) prints 8 and break stops before the final 2.
  • Go's map may resize internally and handles hashing/collisions behind the runtime API, but this source and trace expose only key lookup and count mutation, not bucket-level behavior.