Problem 250531 · easy · Phase 02 Linear Data Structures

Contains Duplicate

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.

Given a list of integers nums, return True if any value appears at least twice, and False if every element is distinct.

Examples

Input:  nums = [1, 2, 3, 1]
Output: True
Input:  nums = [1, 2, 3, 4]
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…