Introduction

Welcome back! Today, we are focusing on solving complex algorithmic problems using Python dictionaries — the data structure that enables us to store data in key-value pairs. This powerful data structure facilitates lightning-fast retrieval and insertion operations, playing a pivotal role in algorithm efficiency. We will be tackling two carefully selected problems designed to highlight the practical uses of dictionaries: "Majority Vote Problem" and "Implement a Keyword Index". These problems serve to illustrate real-world applications of dictionaries, helping us understand them in a deeper, more meaningful way. Let's dive in!

Problem 1: Majority Vote Problem with Dictionaries

Our first problem is about identifying the "majority" element in a list. The "majority element" in a list is an element that appears more than n / 2 times. Given a list of integers, our aim is to identify the majority element.

Problem Actualization

This problem could arise on numerous occasions. Imagine running a survey where each participant selects a number from 1 to n to rate a product. After the survey, you want to find out if there is a feature that received more than n / 2 votes. Or, consider an internet voting system for an online event. You may need to identify if a candidate is leading by more than half the total votes.

Naive Approach
Efficient Approach Explanation

A more efficient approach is to use a Python dictionary to count the occurrences of each element in the list. If the count of any element exceeds n / 2 at any point during our iteration, we immediately return that element as the "majority element". If no such element is found, we return -1 after we've iterated through all elements.

Solution for Problem 1

Let's break down the solution step-by-step:

  • First, let's set up an empty dictionary count_dict:

    count_dict = {}

This dictionary will help us keep track of the count or occurrences of each element in our list.

  • We iterate through the elements of listA. For each element, we increment its count in count_dict. If the count exceeds n / 2, we return that element:

    for element in listA:
        count_dict[element] = count_dict.get(element, 0) + 1
        if count_dict[element] > len(listA) // 2:
            return element

Here, the method dict.get(key, default) returns the value for a key if it exists in the dictionary. If not, it returns the default value.

  • If no majority element is found after the full iteration, we return -1:

    return -1

Such a default value is a common approach in problems where the answer may not exist.

  • The final solution looks like this:
def solution(listA):
    count_dict = {}
    for element in listA:
        count_dict[element] = count_dict.get(element, 0) + 1
        if count_dict[element] > len(listA) // 2:
            return element
    return -1

On a separate note, do you think we can apply defaultdict here? How will the code change in that case?

Congratulations! You have successfully optimized the solution to the majority vote problem using Python dictionaries.

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