• 0

Find the first unique character in a given string or an array


Problem

You are given an a string of characters including duplicates. You need to find the first non-repeating / unique character.

input: foobar
output: f

input: aabbccdef
output: d

input: aabbcc
output: 'No Unique Character Found'

Solution

  • We will iterate over the string and create a frequency array. Note, not a map.
  • This will be a unique array in the sense that the index will be determined based on the input character.
  • Iterate over the input string again and look up the value associated with the character index.
  • Return the first character whose value is 1.

Code