Unique Travel Souvenirs - codepath/compsci_guides GitHub Wiki

U-nderstand

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

  • Q
    • What is the desired outcome?
      • To determine if the counts of each souvenir type are unique.
    • What input is provided?
      • An array of strings souvenirs.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Count the occurrences of each souvenir, then check if these counts are unique.

1) Count the occurrences of each souvenir in `souvenirs` using a dictionary.
2) Track the counts in a set to check for uniqueness.
3) If all counts are unique, return `True`; otherwise, return `False`.

⚠️ Common Mistakes

  • Not properly checking for duplicate counts.

I-mplement

def unique_souvenir_counts(souvenirs):
    # Create a dictionary to count the number of each souvenir
    count_dict = {}
    for souvenir in souvenirs:
        if souvenir in count_dict:
            count_dict[souvenir] += 1
        else:
            count_dict[souvenir] = 1
    
    # Create a set to track the counts we've seen
    seen_counts = set()
    
    # Check if all counts are unique
    for count in count_dict.values():
        if count in seen_counts:
            return False
        seen_counts.add(count)
    
    return True