First Letter to Appear Twice

Practice this coding question on Coding Gurukul.

Problem statement

A classroom scanner writes one lowercase letter whenever a labeled learning station is scanned. The coordinator wants an alert as soon as a station is scanned for the second time. The log is ordered by scan time, so the alert depends on when a repeated visit happens, rather than which station was visited first. Your task Read s from left to right and print the character at the earliest position that has already appeared earlier. At least one repeated character is guaranteed. Stop at the first second occurrence, even if another letter started earlier in the log or eventually occurs more often. Worked example For s = abba, the first a and b are new. The next b has already appeared, so print b. Although a starts earlier and also repeats, its second occurrence comes later. For zz, print z.

Input format

One string s on a single line.

Output format

Print the first character to reach its second occurrence.

Constraints

2 <= length of s <= 100; s contains only lowercase English letters and has at least one repeated letter.

Sample input

abba

Sample output

b

Sign in to run or submit a solution in C++, Java or Python.