Sets and Deduplication
Lesson 14Beginner47 minAssessment-backed

Sets and Deduplication

Use sets to model membership, uniqueness, replay protection, intersections, and duplicate detection in real systems.

What you will be able to do

Explain a set as a membership and uniqueness structure.
Use sets to deduplicate streams, IDs, events, search results, and user actions.
Choose between preserving order, preserving counts, and preserving uniqueness.
Implement set-based solutions in Java, Python, and JavaScript.
State production trade-offs: memory growth, TTL, normalization, distributed consistency, and false confidence.

A set answers one question efficiently: have we seen this value before? That question appears everywhere in production software: duplicate events, replayed webhooks, already-sent notifications, selected IDs, unique visitors, fraud signals, autocomplete candidates, and merge pipelines.

A Set Is About Membership, Not Rich Records

A hash map stores key to value. A set stores keys only. If the product needs to know whether an ID exists, a set is enough. If it needs a count, timestamp, status, or payload, use a map. This distinction keeps implementations smaller and answers clearer.

SkillSkore principle

Use a set when uniqueness or membership is the actual requirement. Do not use it when the product needs counts, ordering, or per-key metadata.

Set membership flow showing incoming IDs, seen set, duplicate decision, and accepted output
A set turns repeated duplicate checks into membership lookups. The trade-off is memory for every distinct key retained.

Scenario: Webhook Deduplication

A payment provider sends webhooks to your service. Network retries can deliver the same event more than once. If your handler blindly processes every webhook, a customer might receive duplicate emails, duplicate credits, or duplicate fulfillment actions. A set of processed event IDs prevents repeated side effects.

RequirementSet roleProduction risk
Ignore duplicate webhookprocessedEventIds contains eventIdThe set must survive process restarts if side effects are serious.
Avoid duplicate notificationssentNotificationIds contains eventId:userIdKey must include enough scope to avoid suppressing valid sends.
Merge search candidatesseenDocumentIds prevents repeated resultsSet preserves uniqueness but not ranking by itself.
Track unique visitorsvisitorIds stores unique identitiesMemory and privacy constraints matter.
Deduplication pipeline showing incoming events, seen check, accept or skip, and downstream side effects
Deduplication is not just an algorithmic cleanup step. In production it protects side effects from retries and repeated input.

Same Pattern in Java, Python, and JavaScript

The following examples return unique events in first-seen order. The set tracks membership. The output list preserves order. This is a common combination: a set for lookup plus a sequence for final presentation.

import java.util.*;

record Event(String id, String payload) {}

class EventDedup {
    static List<Event> uniqueInArrivalOrder(List<Event> events) {
        Set<String> seen = new HashSet<>();
        List<Event> result = new ArrayList<>();

        for (Event event : events) {
            if (seen.add(event.id())) {
                result.add(event);
            }
        }
        return result;
    }
}

Set vs Map vs Array

RequirementBetter structureReason
Need to know if ID existsSetMembership is the dominant operation.
Need metadata per IDMapA value must be attached to the key.
Need counts per IDMap/counterA set loses multiplicity.
Need stable display orderArray/list plus setArray preserves order; set prevents duplicates.
Need sorted unique valuesTree set/sort laterHash sets do not provide sorted order by default.

Deduplication Design Questions

  • What is the identity key: event ID, user ID, normalized email, document ID, or compound key?
  • How long must the system remember the key: request lifetime, session, day, billing period, or forever?
  • What happens on process restart: can duplicates be tolerated, or must the set be persisted?
  • Does deduplication preserve the first item, latest item, highest-priority item, or merged item?
  • Can the set grow without bound, and should it use TTL, windowing, batching, or durable storage?
Set decision matrix for membership, order, count, metadata, and lifetime
Strong answers state not only that a set is used, but how long it lives and what information it intentionally discards.

Cost Model and Limits

Set add, contains, and remove are expected O(1) in common hash-set implementations. But every unique key consumes memory. For a small request-local dedup this is trivial. For a streaming pipeline, unbounded sets become memory incidents unless the key lifetime is bounded.

Assessment trap

A set removes duplicates but also removes information. If duplicates carry meaning, such as frequency, repeated failures, demand, or fraud intensity, a set may hide the signal you need.

Interview signalFrequently Asked Interview Questions

These are canonical interview patterns for this topic, phrased as concrete problem statements. They avoid vague theory questions and prepare you for the exact reasoning interviewers expect: invariant, edge cases, complexity, and trade-off.

  • Given an integer array, return true if any value appears at least twice.
  • Given two arrays, return the unique values that appear in both arrays.
  • Given a stream/list of event IDs, process each unique event once while preserving first-seen order.

Production practiceCompany-Style Practice Examples

These are not claimed as exact company-specific questions. They are the interview patterns repeatedly used by product companies to test whether you can move from brute force to a defensible implementation.

ExampleBrute-force approachStronger solutionProduction notes
Contains duplicateCompare every pair.Track seen values in a set and stop at the first repeat.Expected O(n) time, O(n) memory; set loses duplicate count.
Unique intersectionNested scan and push duplicates.Build a set from one array, scan the other, and collect results in an output set.Clarify whether output order matters.
Webhook replay protectionProcess every event arrival.Remember processed event IDs in a durable set/index.Mention TTL, persistence, locking, and side-effect safety.

Solved modelInterview Question Solutions

Question 1: Contains Duplicate

Given an integer array, return true if any value appears at least twice. The brute-force solution compares every pair. The set solution tracks values already seen and returns as soon as a repeat appears.

import java.util.*;

class ContainsDuplicate {
    static boolean containsDuplicate(int[] nums) {
        Set<Integer> seen = new HashSet<>();
        for (int value : nums) {
            if (!seen.add(value)) return true;
        }
        return false;
    }
}

Question 2: Unique Intersection of Two Arrays

Given two arrays, return the unique values that appear in both arrays. Build a set from one array, scan the other, and add matches to an output set so duplicates do not repeat in the answer.

import java.util.*;

class ArrayIntersection {
    static int[] intersection(int[] a, int[] b) {
        Set<Integer> values = new HashSet<>();
        for (int value : a) values.add(value);

        Set<Integer> result = new HashSet<>();
        for (int value : b) {
            if (values.contains(value)) result.add(value);
        }

        int[] output = new int[result.size()];
        int i = 0;
        for (int value : result) output[i++] = value;
        return output;
    }
}

Question 3: First-Seen Event Deduplication

Given a stream/list of event IDs, process each unique event once while preserving first-seen order. Use a set to remember processed IDs and a list to preserve the order of accepted events. In production, make the set durable if duplicate side effects cannot be tolerated after restart.

import java.util.*;

record Event(String id, String payload) {}

class UniqueEvents {
    static List<Event> unique(List<Event> events) {
        Set<String> seen = new HashSet<>();
        List<Event> result = new ArrayList<>();
        for (Event event : events) {
            if (seen.add(event.id())) result.add(event);
        }
        return result;
    }
}

Solved modelWorked Interview Answer

Return unique events in first-seen order. Use a set for membership and a list for output order. The set answers whether an event ID has appeared; the output list preserves the first accepted event.

import java.util.*;

record Event(String id, String payload) {}

class UniqueEvents {
    static List<Event> unique(List<Event> events) {
        Set<String> seen = new HashSet<>();
        List<Event> result = new ArrayList<>();
        for (Event event : events) {
            if (seen.add(event.id())) result.add(event);
        }
        return result;
    }
}

Hands-on drillTry It Yourself

Practice task

Design deduplication for a webhook handler. Define the key, whether the set is in-memory or durable, how long keys live, and what side effect duplicates would cause.

Failure patternsCommon Mistakes to Avoid

  • Using a set when the product actually needs counts or metadata.
  • Forgetting that set deduplication may not preserve sorted order.
  • Keeping an unbounded set forever in a long-running stream.
  • Choosing an identity key that is too broad or too narrow.
  • Ignoring restart and distributed-processing behavior for serious side effects.

Execution guardrailQuick-Start Checklist

  • Define the membership key.
  • Decide whether order matters.
  • Decide whether counts or metadata matter.
  • Bound the lifetime of stored keys.
  • State expected runtime, memory cost, and failure mode.

Recall drillKnowledge Check

QuestionStrong answer
What question does a set answer?Whether a value is a member of the collection.
What information does a set discard?Duplicate count, arrival count, and per-key metadata unless stored elsewhere.
Why can sets be risky in streams?Distinct keys can grow without bound unless lifetime is limited.

VocabularyKey Terms

TermMeaning
MembershipWhether a value exists in a collection.
DeduplicationRemoving or suppressing repeated values according to an identity rule.
First-seen orderPreserving the order in which unique values first appeared.
TTLTime to live: how long a remembered key remains valid.
Compound keyA key built from multiple fields to represent exact identity.

Next practiceFurther Reading

  • Practice contains-duplicate, intersection, and first-duplicate problems.
  • Review idempotency and replay protection in event-driven systems.
  • Study Bloom filters later for approximate membership under memory pressure.