HashSet.add() Return Value
Fast recall
Quick revision
Core answer
contains() and then add().Knowledge of Set API contract and practical usage patterns for duplicate detection
Ignoring the return value and making a separate contains call before adding, resulting in two operations instead of one
Demonstrate the classic find-first-duplicate algorithm that uses the boolean return to detect the first repeated element in O(n) time
Interview-ready guide
Interview answer
The add method in HashSet returns a boolean that tells you if the element was actually added.
It returns true when the element is new and gets inserted, and false when it already existed in the set. This is really useful because you can detect duplicates in one operation instead of two.
A common mistake freshers make is checking if the set contains an element first, then adding it. That's two hash lookups when you only need one. You can just call add and check if it returns false, which means you found a duplicate.
There's a classic interview problem where you need to find the first duplicate in a list. The efficient solution uses this return value. You loop through the list, try to add each element to a HashSet, and the first time add returns false, that's your first duplicate.
It's order n time and space, really clean. The return value comes from the underlying HashMap that HashSet uses internally. When HashMap inserts a new key, HashSet translates that into returning true.
When the key already exists, that becomes false. So you're getting direct feedback about whether the element was unique or not. It's a small detail, but using it properly makes your code both faster and cleaner.
HashSet.add() Return Value
Direct Answer
HashSet.add(e) returns a boolean: true when the element was new and got inserted, false when it already existed and the set was unchanged. Use that return value to detect duplicates in a single hash lookup instead of calling contains() and then add().
Code Example
The classic interview use is finding the first repeated value in one pass. Read the return value of add() and stop as soon as it is false.
import java.util.*;
public class DuplicateDetection {
// Find the first duplicate in O(n) time, O(n) space
static Long findFirstDuplicate(List<Long> orderIds) {
var seen = new HashSet<Long>();
for (var id : orderIds) {
if (!seen.add(id)) { // add returned false -> duplicate
return id;
}
}
return null; // no duplicates
}
public static void main(String[] args) {
var orders = List.of(101L, 102L, 103L, 102L, 104L);
System.out.println("First duplicate: " + findFirstDuplicate(orders)); // 102
// Process each id exactly once -> act only when add returns true
var processed = new HashSet<Long>();
for (var id : orders) {
if (processed.add(id)) {
System.out.println("Processing: " + id);
}
}
}
}
The !seen.add(id) line is the trick: one call inserts the id and tells you whether it was already there, with no extra contains() lookup.
Common Mistakes & Pitfalls
- Ignoring the return value entirely: writing
contains()beforeadd()makes two hash lookups instead of one. It is slower and more verbose for no gain — letadd()'s return value do the work. - Reading the boolean backwards: some freshers think
add()returnstruewhen the element already existed. It is the opposite —truemeans it was new and got inserted. Reading it backwards breaks every duplicate-detection loop. - Discarding the result in a stream: collecting into a set already drops duplicates, but if you need to act on the first repeat, you must capture the boolean from
add()rather than throwing it away.
Check your understanding
Answer these without looking back at the article.
- How do you find the first duplicate element in an array using HashSet?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.