Code Logo

First Unique Customer with LinkedHashMap

Published at22 Apr 2026
Collections Framework Medium 0 views
Like0

A cafe keeps a simple arrival log of customer names during a busy lunch period. Some people appear more than once because they stepped away from the counter and came back later, but the manager wants to know which customer was the first one whose name appears only a single time in the whole list.

Your task is to return the first name that occurs exactly once. If every name repeats, return "None".

For example, if the log is ["Ayu", "Bima", "Ayu", "Cici", "Bima", "Dodi"], the answer should be "Cici". Even though Dodi also appears once, Cici is the first single-occurrence name when you read the list from left to right.

This problem is a good fit for LinkedHashMap because you need two things at the same time: counting occurrences and preserving insertion order. A plain map can count names, but a linked map keeps the order in which keys were first added, which is exactly what the question cares about.

The result is not just about frequency. It is about the earliest name among those with frequency one, so both counting and order have to stay visible in the solution.

Example Input & Output

Example 1
Input
names = ["Tio", "Tio", "Uli", "Uli"]
Output
"None"
Explanation

If every name repeats, return None.

Example 2
Input
names = ["Nina", "Nina", "Raka"]
Output
"Raka"
Explanation

Raka is the only name with frequency one.

Example 3
Input
names = ["Ayu", "Bima", "Ayu", "Cici", "Bima", "Dodi"]
Output
"Cici"
Explanation

Cici is the first name that appears exactly once.

Algorithm Flow

Recommendation Algorithm Flow for First Unique Customer with LinkedHashMap
Recommendation Algorithm Flow for First Unique Customer with LinkedHashMap

Solution Approach

The key challenge here is that one pass of simple comparison is not enough. You need to know how many times every name appears, but after that you still need to respect the order of first appearance. That is why LinkedHashMap is a better fit than a regular HashMap for the main counting structure.

A clean plan is to loop through the names once and build a linked map from each name to its count. Since LinkedHashMap preserves insertion order for keys, the map remembers the order in which each distinct customer name first appeared.

Then you loop through the map entries and return the first key whose count is 1. That gives you the earliest unique customer without needing extra sorting or complicated index tracking.

The structure looks like this:

Map<String, Integer> counts = new LinkedHashMap<>();
for (String name : names) {
    counts.put(name, counts.getOrDefault(name, 0) + 1);
}
for (Map.Entry<String, Integer> entry : counts.entrySet()) {
    if (entry.getValue() == 1) {
        return entry.getKey();
    }
}
return "None";

This works because the first loop solves the frequency problem and the second loop solves the order problem. Together they match the requirement exactly.

The runtime is O(n) because each name is processed a constant number of times, and the extra space is O(k) for the distinct names stored in the map. The important lesson is that collection choice matters when both counting and insertion order are part of the problem statement.

Best Answers

java - Approach 1
import java.util.List;
import java.util.Map;
import java.util.LinkedHashMap;

class Solution {
    public static String firstUniqueCustomer(List<String> names) {
        Map<String, Integer> counts = new LinkedHashMap<>();
        for (String name : names) {
            counts.put(name, counts.getOrDefault(name, 0) + 1);
        }
        for (Map.Entry<String, Integer> entry : counts.entrySet()) {
            if (entry.getValue() == 1) {
                return entry.getKey();
            }
        }
        return "None";
    }
}