Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

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


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *