First Letter Seen Twice

EasyHash Map≈ 10 min

You are given a string text. Read it from left to right and return the first character you meet for the second time. If no character appears twice, return an empty string "".

The text may hold any characters: letters from any alphabet, digits, spaces and punctuation. Upper and lower case are different characters, so "A" and "a" are not a repeat.

Examples

text
text = "abccba"       →  "c"
text = "programming"  →  "r"
text = "world"        →  ""
text = "a-b-c"        →  "-"

In "programming", the letters p, r, o, g are new; the fifth character, r, has been seen before. The two ms and two gs repeat later, so they do not count.

Constraints

  • Between 0 and 20,000 characters.
  • Aim for O(n): look at each character once.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 4 visible cases here, in your browser. Submit runs all 9 on the judge, 5 of them hidden, and measures how your solution scales.