v9.1.1
Menu

Contains Duplicate

easyFREEArrays & HashingBlind 75

Given an integer array nums, return True if any value appears at least twice in the array, and False if every element is distinct.

Example

Input
1,2,3,1
Output
True
Reasoning
  • Input: nums is a list of integers; the sample 1,2,3,1 means nums =[1,2,3,1]= [1, 2, 3, 1], so n=4n = 4 and the positions are 0,1,2,30, 1, 2, 3.
  • The question: do two different positions i≠ji \neq j hold the same value, i.e. is there a pair with numsi=numsj\text{nums}_i = \text{nums}_j?
  • Checking the pairs: positions 00 and 33 both hold 11, so nums0=nums3=1\text{nums}_0 = \text{nums}_3 = 1 with 0≠30 \neq 3. One such pair is enough, and the answer is True.
  • Contrast with [1,2,3,4][1, 2, 3, 4]: all four values differ, so no pair of positions matches and the answer is False.
  • Output: a single boolean, not a count and not the repeated value. In [1,1,1,3,3,4,3,2,4,2][1, 1, 1, 3, 3, 4, 3, 2, 4, 2] several values repeat, sometimes three times, yet the output is still just True.
  • Notice that the two equal values need not be adjacent and order is irrelevant: the two 11s here sit at opposite ends of the list.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9

More from Blind 75

solution.py

Test results

Sign in to run your code against the test cases.