Problem 250531 · easy · Level 02 Linear Data Structures

Was a Ticket Sold Twice?

sets · hash maps

A set stores values with no duplicates and answers x in my_set in constant time, no matter how big it is. A list answers the same question by scanning every element. That difference turns many slow nested loops into a single fast pass.

A box office suspects its ticket printer of a fault. The serial numbers of the tickets it sold are in nums, and a working printer never issues the same number twice. Return True if some serial number occurs more than once, and False if all of them are different.

Examples

Input:  nums = [104, 220, 317, 220]
Output: True
Input:  nums = [5, 18, 42, 7]
Output: False

Constraints

  • 0 <= len(nums) <= 10**5
  • -10**9 <= nums[i] <= 10**9

Goals

  • Use a set for O(1) membership checks
  • Replace a nested loop with a single pass plus extra memory
Starting Python…