Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Detect duplicates with a frequency map

Find elements that appear more than once using a frequency map and return them in first-duplicate order.

Python practice22 minHashing & SetsAdvancedLast updated March 26, 2026

Problem statement

Given an array arr, return a list of the elements that occur more than once. Each duplicate element should appear exactly once in the output list, in the order when its second occurrence is encountered (i.e., the order in which elements first become duplicates). If there are no duplicates, return an empty list.

Task

Build a frequency map and output elements that reach frequency >= 2, preserving the order in which they first become duplicates.

Examples

Multiple duplicates

Input

arr = [3, 1, 2, 3, 2]

Output

[3, 2]

3 becomes a duplicate when the second 3 is seen, then 2 becomes a duplicate when the second 2 is seen. Order: [3, 2].

Input format

Function input: find_duplicates(arr) where arr is a list of integers (or hashable values).

Output format

Return a list containing each duplicated element exactly once, in the order they first become duplicates.

Constraints

0 <= len(arr) <= 10^5. Elements are hashable (ints, strings, etc.). Aim for O(n) time and O(n) space.

Samples

Sample 1

Input

arr = [1, 1, 1, 2]

Output

[1]

1 becomes a duplicate at the second 1, but subsequent repeats are not added again.