Skip to Content
Course content

94: HashSet and BTreeSet

Click on the "Edit" button in the top corner of the screen to edit your slide content.

A few years ago, I was helping a teammate optimize a log analysis tool that flagged unique IP addresses during a suspected DDoS attack. The original code was simple: it pushed every incoming IP into a Vec and then called contains() to see if they'd seen that IP before. It worked perfectly in the staging environment with a few thousand logs. But the moment it hit production with millions of entries, the tool practically ground to a halt. The time complexity was O(n) for every single check; as the list grew, the program spent all its time scanning the same list over and over.

The fix took about thirty seconds. We swapped the Vec for a HashSet, and the execution time dropped from minutes to milliseconds. That's the power of choosing the right data structure for the job. In Rust, when you care about uniqueness and fast lookups, you want a Set.

Fast Lookups with HashSet

A HashSet is essentially a HashMap where you only care about the keys, not the values. It uses a hashing function to map your data to a specific bucket in memory, allowing it to check if an item exists in constant time—O(1)—regardless of whether you have ten items or ten million.

I usually reach for HashSet by default because it's incredibly efficient. However, there is one trade-off: it doesn't preserve the order of insertion. If you iterate over a HashSet, the elements will come out in a seemingly random order. Here is how you'll typically use it in a real project:

use std::collections::HashSet;

fn main() {
    let mut visitor_ips = HashSet::new();
    
    // These will all be added
    visitor_ips.insert("192.168.1.1");
    visitor_ips.insert("10.0.0.1");
    
    // This is a duplicate; insert returns false and does nothing
    let was_new = visitor_ips.insert("192.168.1.1"); 
    
    println!("Was the IP new? {}", was_new); // false
    println!("Unique visitors: {}", visitor_ips.len());
}

Ordering Data with BTreeSet

Now, there are times when that "random" order of a HashSet is a dealbreaker. Maybe you're building a leaderboard, or a system that needs to output a sorted list of unique usernames. This is where BTreeSet comes in. Instead of hashing, it uses a B-Tree internally to keep your elements sorted.

The trade-off here is performance. A BTreeSet has O(log n) time complexity for insertions and lookups. While that's slower than the O(1) of a HashSet, it's still vastly superior to a Vec for uniqueness checks. More importantly, it gives you the ability to perform range queries—like finding all elements greater than X but less than Y—which is impossible with a hash-based set.

use std::collections::BTreeSet;

fn main() {
    let mut scores = BTreeSet::new();
    scores.insert(50);
    scores.insert(10);
    scores.insert(100);
    scores.insert(50); // Duplicate, ignored

    // BTreeSet iterates in sorted order automatically
    for score in &scores {
        println!("{}", score); 
    }
    // Output: 10, 50, 100
}

If you're undecided, ask yourself: "Do I need this list to be sorted?" If the answer is no, stick with HashSet. If you need sorting or range-based lookups, BTreeSet is your tool.




📋 Practical Task

Build a Unique Ingredient Auditor

You are building a nutrition app that merges ingredients from multiple recipe versions. Sometimes different versions of a recipe list the same ingredient twice (e.g., "Salt" listed in the prep and the simmer stages). You need to create a tool that takes a list of ingredients and returns a sorted, unique list of everything required for the shopping list.

Requirements:

  • Create a function audit_ingredients(ingredients: Vec<String>) -> Vec<String>.
  • The function should remove all duplicate ingredients.
  • The final list must be returned in alphabetical order.
  • Use the most appropriate Set implementation to handle both uniqueness and sorting efficiently.
fn main() {
    let recipe_v1 = vec!["Flour".to_string(), "Sugar".to_string(), "Butter".to_string()];
    let recipe_v2 = vec!["Sugar".to_string(), "Eggs".to_string(), "Flour".to_string()];
    
    let mut all_ingredients = recipe_v1;
    all_ingredients.extend(recipe_v2);
    
    let shopping_list = audit_ingredients(all_ingredients);
    println!("{:?}", shopping_list); 
    // Expected: ["Butter", "Eggs", "Flour", "Sugar"]
}
Rating
0 0

There are no comments for now.

to be the first to leave a comment.