Return the first recurring character in a string The task:


  Given a string, return the first recurring character in it, or null if
  there is no recurring character.
  
  For example, given the string "acbbac", return "b". Given the string
  "abcdef", return null.


My imperative solution:

function getFirstRecurringChar(str) {
  let i = 0;
  const len = str.length;
  const letters = {};
  while(i &lt; len &amp;&amp; !letters[str[i]]) { letters[str[i++]] = true; }
  return i &lt; len ? str[i] : null;
};

console.log(getFirstRecurringChar("acbbac"));


My functional solution:

const getFirstRecurringChar2 = str =&gt; {
  const letters = {};
  let lastLetter;
  return Array.from(str).some(x =&gt; {
    if(letters[x]) {
      lastLetter = x;
      return true;
    }
    letters[x] = true;
    return false;
  }) ? lastLetter : null;
};

console.log(getFirstRecurringChar2("abcdef"));
The problem is similar to your other "two of something" problems, such as Palindrome and Find the elements that appear only once.  As in those problems, a Set is the appropriate data structure.

The main difference is that early exit is possible (when two of something is found) so we'd rather not be inside of a reduce. 

const firstRepeated = s =&gt; {
  const seen=new Set();
  for (var c of s) {
      if (seen.has(c)) return c; 
      else seen.add(c); 
  }
  return null;
}