00:00

Problem Statement

Input/Output Examples

Visual Explanation

The method needs the full count before it can decide, because a character that looks unique early may appear again later. That is why the solution makes two passes: one to count and one to collect.

Approaches

Both approaches count first and collect second. They differ in the counting structure.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the length of the string. Both approaches make two linear passes. The map grows with the number of distinct characters, while the array always has 128 slots.

Interview Tips and Recommended Approach

  • Clarify the output order. First appearance needs a LinkedHashMap or a second pass over the string; a plain HashMap gives no order.
  • Report each duplicate once. Printing inside the first loop would print e twice for "committee".
  • Ask about case and spaces. Should J and j count as the same character?
  • Avoid nested loops. Comparing every pair of characters works but costs O(n²).

Recommended Approach: LinkedHashMap Counts

Why: it works for every character, keeps the natural first-appearance order and runs in linear time with simple code.