217. Contains Duplicate

Contains Duplicate (LeetCode 217) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Hashing - Previously Seen / Existence Check subpattern.

How to solve Contains Duplicate

Contains Duplicate is a Hashing for Existence Checks and Seen Tracking problem, part of Array/Matrix Manipulation Patterns. Use hash sets for O(1) existence checks. Track previously seen elements to detect duplicates, find pairs, and validate sequences.

Work towards the solution in these steps:

  1. Keep track of values encountered while scanning the array.
  2. Before recording the current value, check whether it has already been encountered.
  3. A hash-based set supports the needed membership check efficiently.

The reference solution runs in O(n) time and O(n) space.

The Contains Duplicate solution and editorial walks through the full approach with reference code.

Problem statement

Given nums, determine whether any integer occurs at least twice in the array. Two occurrences count as a duplicate even if they are at different positions.

Return true if nums contains a repeated value. Return false if every value appears exactly once.

Example 1:

Input: nums = [8,3,5,8] Output: true Explanation: The value 8 appears at two positions, so the array contains a duplicate.

Example 2:

Input: nums = [-4,12,0,7] Output: false Explanation: All four values are different, so no duplicate exists.

Example 3:

Input: nums = [6,6] Output: true Explanation: Both elements have the same value, which is enough to make the result true.

Constraints:

- 1 <= nums.length <= 10^5 - -10^9 <= nums[i] <= 10^9 - nums contains only integers.

Topics and companies

Topics: Array, Hash Table.

How to practise Contains Duplicate on Thita.ai

Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(n) space.

Open Contains Duplicate in the code editor, or read the Contains Duplicate editorial for a worked solution with its approach and complexity analysis.

Where Contains Duplicate sits in the DSA pattern sheet

Problems related to Contains Duplicate

Other problems that use the same Hashing - Previously Seen / Existence Check and Array/Matrix Manipulation Patterns techniques:

Contains Duplicate FAQ

What is the LeetCode number of Contains Duplicate?

Contains Duplicate is LeetCode problem 217, rated Easy.

What pattern is used to solve Contains Duplicate?

Contains Duplicate is a Hashing for Existence Checks and Seen Tracking problem. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Hashing - Previously Seen / Existence Check subpattern.

What is the time complexity of Contains Duplicate?

The reference solution runs in O(n) time and O(n) space.

Preparing this workspace.