Introduction to the Lesson

Welcome to the lesson on using sets in Python. We'll utilize Python hash sets to solve common algorithmic problems that a software engineer might encounter daily. Specifically, we'll focus on problems such as identifying the intersection of two lists, picking out elements that appear only once in a list, and determining elements unique to individual lists.

Problem 1: Array Intersection

Our journey begins with the challenge of identifying the intersection of two arrays. In other words, we aim to pinpoint the elements that appear in both of the given lists. It's important to note that we're interested in locating unique common elements - even if an element appears more than once in both lists, it should only feature once in our output.

Problem 1: Problem Actualisation

To elucidate how this problem might emerge in a real-world scenario, presume that you're managing a database for a marketing company. You have two customer lists, each obtained through various marketing strategies. Your task is to determine the customers that both strategies successfully targeted. Essentially, these are the common elements in your two lists.

Problem 1: Naive Approach

Suppose you decide to resolve this problem in the most uncomplicated way possible: for each customer (or element) on the first list, you verify if they’re also present on the second list. Once you identify a match, you must confirm that this customer hasn't previously been added to your output. Though this solution would, in the end, yield the correct list of shared customers, it would demand a lot of computational resources, as you would be operating at a time complexity of O(n2)O(n^2) due to the nested lookups – far from ideal!

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