Skip to content
Learn Netverks

Lesson

Step 21/36 58% through track

sets-maps-usage

Sets and maps usage

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Sets and maps usage: data structure and algorithm concepts with complexity analysis and interview-ready C++ examples.

Teams apply Sets and maps usage in every serious DSA project—skipping it leaves blind spots in analysis and reviews.

You will apply Sets and maps usage in contexts like: Deduplication, frequency counts, and O(1) lookups in services and ETL.

Compile and run C++17 snippets in the playground (`int main`, `std::cout`); after each run, state time and space complexity before moving on.

When you can explain the previous lesson's ideas in your own words.

std::set and std::map keep keys sorted in a balanced tree—O(log n) operations. Use when you need ordered traversal or guaranteed log time without hash assumptions.

Container guide

  • unordered_set — unique keys, average O(1)
  • set — unique sorted keys, O(log n)
  • map — sorted key → value
  • unordered_map — hash key → value

Duplicate detection

#include 
#include 
#include 

int main() {
    std::vector a = {1, 2, 3, 2, 4};
    std::unordered_set seen;
    for (int x : a) {
        if (seen.count(x)) {
            std::cout << "duplicate: " << x << "\n";
            break;
        }
        seen.insert(x);
    }
    return 0;
}

Ordered map iteration

std::map iterates keys in order—useful for sweep-line and coordinate compression previews.

Important interview questions and answers

  1. Q: set vs unordered_set?
    A: set sorted O(log n); unordered_set average O(1) unsorted.
  2. Q: multiset?
    A: Allows duplicate keys with count—frequency beyond simple set.

Self-check

  1. When pick map over unordered_map?
  2. How detect first duplicate in O(n) average?

Pitfall: Using map when you only need fast lookup—unordered_map may be faster average case.

Interview prep

map vs unordered_map?

map sorted O(log n); unordered_map hash average O(1).

Duplicate check?

unordered_set insert—fail if exists.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • set vs map?
  • Duplicate detect?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump