Introduction to the Lesson

Welcome back! Today, we're sharpening our algorithmic problem-solving skills, focusing on std::unordered_map in C++. Imagine std::unordered_map as the backbone of a social network app, ensuring that each user's data remains unique and instantly accessible. By the close of this lesson, you will be equipped to use C++'s std::unordered_map to efficiently solve problems that might otherwise seem daunting due to their complexity or data size.

Problem 1: Majority Element Finder

Our journey begins with the Majority Element Finder. You're handed an array of integers, and your mission is simple yet intriguing: determine whether this array has a celebrity element. This integer appears more frequently than all others combined. More formally, we are looking for an element appearing more than n/2 times.

Why does this matter? Well, consider analyzing sales data to determine the most sold product in an online marketplace — knowing the majority of products could streamline marketing strategies and inventory management. That's the real-world relevance of the majority element problem.

Now, let's be savvy about this. Enter the std::unordered_map: your sophisticated voting tally system. With it, you can keep a running total of each product's sales as you go through the transaction list once rather than reviewing the entire list for each product. It grants us the speed of an experienced cashier who knows customers' habits by heart.

Problem 1: Solution Building

Let's dissect the process in our election analogy step by step:

#include <unordered_map>
#include <vector>

int FindMajorityElement(const std::vector<int>& arr) {
    std::unordered_map<int, int> countMap;
    int majorityThreshold = arr.size() / 2;

Here, we're preparing our std::unordered_map, akin to setting up our ballot boxes and establishing the majority threshold — the number of votes needed to win the election by a landslide.

    for (const auto& num : arr) {
        countMap[num] += 1;

We're recording every vote cast in our std::unordered_map ledger. Each key is a unique product, and the value is the current total of received votes.

        if (countMap[num] > majorityThreshold) {
            return num;
        }
    }

    return -1; // No majority element found
}

Once a product's vote count exceeds the threshold — signaling a majority — our search concludes, akin to declaring a winner! If the polling ends with no majority victor, we return -1.

Full Code:

#include <unordered_map>
#include <vector>

int FindMajorityElement(const std::vector<int>& arr) {
    std::unordered_map<int, int> countMap;
    int majorityThreshold = arr.size() / 2;

    for (const auto& num : arr) {
        countMap[num] += 1;

        if (countMap[num] > majorityThreshold) {
            return num;
        }
    }

    return -1; // No majority element found
}
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal