CodePark/ problems / First Repeated Character
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Foundations

First Repeated Character

easyabout 10 minutes in an interview

Write firstRepeat(s) that reads the string s from left to right and returns the first character that you have already seen earlier in the string.

  • In "abba" the answer is "b". The second b (index 2) comes before the second a (index 3).
  • Characters are compared exactly: "A" and "a" are different. Spaces and punctuation count as characters.
  • If no character appears twice, return null.
  • Aim to read the string only once.
Input: s = "radar" Output: "a"Reading left to right: r, a, d, then a again. The a repeats before the r does.
Input: s = "abba" Output: "b"
Input: s = "abc" Output: null