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 Ruby 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.rb
Replay: real traced execution (multi-file project)
arr = [3, 5, 2, 5, 3, 8, 2]
count = Hash.new(0)
arr.each { |value| count[value] += 1 }
arr.each do |value|
  if count[value] == 1
    puts value
    break
  end
end
  1. arr ← [3, 5, 2, 5, 3, 8, 2]

    1arr = [3, 5, 2, 5, 3, 8, 2]2count = Hash.new(0)
    values this step[3, 5, 2, 5, 3, 8, 2]arr
  2. count ← {}

    1arr = [3, 5, 2, 5, 3, 8, 2]2count = Hash.new(0)3arr.each { |value| count[value] += 1 }
    values this step{}count
  3. count ← {3: 1}

    1arr = [3, 5, 2, 5, 3, 8, 2]2count = Hash.new(0)3arr.each { |value| count[value] += 1 }
    values this step{} {3: 1}count3value
  4. count ← {3: 1, 5: 1}

    1arr = [3, 5, 2, 5, 3, 8, 2]2count = Hash.new(0)3arr.each { |value| count[value] += 1 }
    values this step{3: 1} {3: 1, 5: 1}count5value
  5. count ← {3: 1, 5: 1, 2: 1}

    1arr = [3, 5, 2, 5, 3, 8, 2]2count = Hash.new(0)3arr.each { |value| count[value] += 1 }
    values this step{3: 1, 5: 1} {3: 1, 5: 1, 2: 1}count2value
  6. count ← {3: 1, 5: 2, 2: 1}

    1arr = [3, 5, 2, 5, 3, 8, 2]2count = Hash.new(0)3arr.each { |value| count[value] += 1 }
    values this step{3: 1, 5: 1, 2: 1} {3: 1, 5: 2, 2: 1}count5value
  7. count ← {3: 2, 5: 2, 2: 1}

    1arr = [3, 5, 2, 5, 3, 8, 2]2count = Hash.new(0)3arr.each { |value| count[value] += 1 }
    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}

    1arr = [3, 5, 2, 5, 3, 8, 2]2count = Hash.new(0)3arr.each { |value| count[value] += 1 }
    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}

    1arr = [3, 5, 2, 5, 3, 8, 2]2count = Hash.new(0)3arr.each { |value| count[value] += 1 }
    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

    4arr.each do |value|5  if count[value] == 16    puts 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

    4arr.each do |value|5  if count[value] == 16    puts 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

    4arr.each do |value|5  if count[value] == 16    puts 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

    4arr.each do |value|5  if count[value] == 16    puts 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

    4arr.each do |value|5  if count[value] == 16    puts 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

    4arr.each do |value|5  if count[value] == 16    puts value
    values this step5i8value1count[value]yesfound8result{3: 2, 5: 2, 2: 2, 8: 1}count
  16. stdout ← 8

    1arr = [3, 5, 2, 5, 3, 8, 2]2count = Hash.new(0)
    values this step8stdout8result

Complexity

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

Implementation notes

  • arr is a Ruby array of integers, not a string, so both passes iterate values with arr.each.
  • count = Hash.new(0) creates a Ruby Hash whose missing keys read as 0.
  • The counting pass uses count[value] += 1; the default value makes the first increment write 1 without a separate key check.
  • After the first pass, the trace shows the frequency table as {3: 2, 5: 2, 2: 2, 8: 1}.
  • The second pass scans the original array order again, checking count[value] == 1.
  • Values 3, 5, 2, 5, and 3 are skipped because their counts are 2.
  • When the scan reaches value 8 at index 5, the source prints it with puts value and exits the loop with break.
  • The checked output is the single line 8; the source never prints the hash table itself.