Back to all solutions

#387 - First Unique Character in a String

Problem Description

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.

Solution

/**
 * @param {string} s
 * @return {number}
 */
var firstUniqChar = function(s) {
  for (let i = 0; i < s.length; i++) {
    if (s.indexOf(s.charAt(i)) === s.lastIndexOf(s.charAt(i))) {
      return i;
    }
  }

  return -1;
};