Updated 14 days ago | GitHub
On this page

First Unique Items

TIP101 Unit 2 Session 1 (Click for link to problem statements)

U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • What are the inputs?
    • Two lists, list_a and list_b.
  • What should the function return?
    • A dictionary of the items that appear in only one of the lists. An item found only in list_a maps to True, and an item found only in list_b maps to False.
  • What about items that appear in both lists?
    • Leave them out of the dictionary.
HAPPY CASE
Input: list_a = [1, 2, 3], list_b = [3, 4]
Output: {1: True, 2: True, 4: False}
Explanation: 1 and 2 are only in list_a, 4 is only in list_b, and 3 is in both lists, so it is left out.

EDGE CASE
Input: list_a = [1, 2], list_b = [2, 1]
Output: {}
Explanation: Every item appears in both lists, so no item is unique.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Find items unique to one list, and categorize based on which list they were unique to.

1) Create an empty dict for unique 
2) For each item in list_a:
  a) If item is not in list_b, map item -> True in unique dict
3) For each item in list_b:
  a) If item is not in list_a, map item -> False in unique dict
4) Return unique dict

I-mplement

def find_unique_items(list_a, list_b):
    unique = {}  # Initialize an empty dictionary to store unique items and their origin (True for list_a, False for list_b)

    # Check for items in list_a not in list_b
    for item in list_a:
        if item not in list_b:
            unique[item] = True

    # Check for items in list_b not in list_a
    for item in list_b:
        if item not in list_a:
            unique[item] = False

    return unique