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 Fortran DSA implementation can be compared directly with the rest of the DSA track.

Basic Implementation

basic.f90
program main
  implicit none
  integer :: arr(7) = [3, 5, 2, 5, 3, 8, 2]
  integer :: count(10) = 0
  integer :: i
  do i = 1, 7
    count(arr(i)) = count(arr(i)) + 1
  end do
  do i = 1, 7
    if (count(arr(i)) == 1) then
      print '(I0)', arr(i)
      exit
    end if
  end do
end program main

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.
  • The trace highlights the hash table state after each write.
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.