First Unique Character in a String The task

is taken from leetcode


  Given a string, find the first non-repeating character in it and
  return it's index. If it doesn't exist, return -1.
  
  Examples:
  
  s = "leetcode"
  
  return 0.
  
  s = "loveleetcode",
  
  return 2.
  
  Note: You may assume the string contain only lowercase letters.


My solution

/**
 * @param {string} s
 * @return {number}
 */
function firstUniqChar(s) {
  const arr = Array.from([...s]
                         .reduce((map, x, i) =&gt; map.set(x, !isNaN(map.get(x)) ? null : i) , new Map())
                         .values());
  for(let i = 0; i &lt; arr.length; i++) {
    if (arr[i] !== null) { return arr[i]; }
  }
  return -1;
};
Your solution looks rather map-happy.

It is important to remember for this task that counting higher than 2 of any encountered letter is needless processing as is any processing on any letter after the earliest positioned unique letter.

Also, doing a complete sweep of the input string may be ill-advised if the input string is of considerable length.

Due to not being as well across .js as others here are, I'll post a humble for loop.

function firstNonRepeatedCharacterPosition(string) {
    for (let char, pos, i = 0; i &lt; string.length; ++i) {
        char = string.charAt(i);
        pos = string.indexOf(char);
        if (pos == i &amp;&amp; string.indexOf(char, i + 1) == -1) {
            return pos;
        } 
    }
    return -1;
}

console.log(firstNonRepeatedCharacterPosition('abcbebc'));


It does make 3 function calls per iteration, but they aren't heavy ones, there is at most only one pass through the string, and early-return programming is in effect.


Grab the letter at the incremented position. 
Find the earliest occurrence of that letter.
Check if there is a later occurrence of the same letter.


The later the unique letter exists (or if there are no unique letters), the more laborious my function is.  On the other hand, if the first letter is unique, then you are finished in just 3 function calls.

p.s. I lack the knowledge to interpret your js code, so I cannot review it beyond saying that it isn't very novice friendly.



After Rotora's challenge, I was doubting myself, so I whacked this little battery of tests together:



function MickMacKusa(string) {
    for (let char, pos, i = 0; i &lt; string.length; ++i) {
        char = string.charAt(i);
        pos = string.indexOf(char);
        if (pos == i &amp;&amp; string.indexOf(char, i + 1) == -1) {
            return pos;
        } 
    }
    return -1;
}

function RoToRa(string) {
    for (let char, pos, i = 0; i &lt; string.length; ++i) {
        char = string.charAt(i);
        if (string.indexOf(char, i + 1) == -1) {
            return i;
        } 
    }
    return -1;
}

let table = document.getElementById("test"),
    row;
for (let i = 1; i &lt; table.rows.length; ++i) {
    row = table.rows[i];
    row.cells[3].innerHTML = MickMacKusa(row.cells[0].innerHTML);
    row.cells[4].innerHTML = RoToRa(row.cells[0].innerHTML);
}
&lt;table id="test" border="1" cellpadding="4"&gt;
&lt;tr&gt;&lt;th&gt;Input&lt;/th&gt;&lt;th&gt;Letter&lt;/th&gt;&lt;th&gt;Index&lt;/th&gt;&lt;th&gt;MickMacKusa&lt;/th&gt;&lt;th&gt;RoTora&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;abccbcba&lt;/td&gt;  &lt;td&gt;-&lt;/td&gt; &lt;td&gt;-1&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;abcbebc&lt;/td&gt;   &lt;td&gt;a&lt;/td&gt; &lt;td&gt; 0&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;abc&lt;/td&gt;       &lt;td&gt;a&lt;/td&gt; &lt;td&gt; 0&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;aabbc&lt;/td&gt;     &lt;td&gt;c&lt;/td&gt; &lt;td&gt; 4&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;abcba&lt;/td&gt;     &lt;td&gt;c&lt;/td&gt; &lt;td&gt; 2&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;abba&lt;/td&gt;      &lt;td&gt;-&lt;/td&gt; &lt;td&gt;-1&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;abaa&lt;/td&gt;      &lt;td&gt;b&lt;/td&gt; &lt;td&gt; 1&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;aabcbcbca&lt;/td&gt; &lt;td&gt;-&lt;/td&gt; &lt;td&gt;-1&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;