Skip to content

Commit a7ad8ba

Browse files
committed
feat: add Contains Duplicate
1 parent 63dce89 commit a7ad8ba

File tree

7 files changed

+191
-1
lines changed

7 files changed

+191
-1
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"problem_name": "contains_duplicate",
3+
"solution_class_name": "Solution",
4+
"problem_number": "217",
5+
"problem_title": "Contains Duplicate",
6+
"difficulty": "Easy",
7+
"topics": "Array, Hash Table, Sorting",
8+
"tags": ["grind-75"],
9+
"readme_description": "Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.",
10+
"readme_examples": [
11+
{
12+
"content": "```\nInput: nums = [1,2,3,1]\nOutput: true\n```\n**Explanation:** The element 1 occurs at the indices 0 and 3."
13+
},
14+
{
15+
"content": "```\nInput: nums = [1,2,3,4]\nOutput: false\n```\n**Explanation:** All elements are distinct."
16+
},
17+
{ "content": "```\nInput: nums = [1,1,1,3,3,4,3,2,4,2]\nOutput: true\n```" }
18+
],
19+
"readme_constraints": "- 1 <= nums.length <= 10^5\n- -10^9 <= nums[i] <= 10^9",
20+
"readme_additional": "",
21+
"solution_imports": "",
22+
"solution_methods": [
23+
{
24+
"name": "contains_duplicate",
25+
"parameters": "nums: list[int]",
26+
"return_type": "bool",
27+
"dummy_return": "False"
28+
}
29+
],
30+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution",
31+
"test_class_name": "ContainsDuplicate",
32+
"test_helper_methods": [
33+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
34+
],
35+
"test_methods": [
36+
{
37+
"name": "test_contains_duplicate",
38+
"parametrize": "nums, expected",
39+
"parametrize_typed": "nums: list[int], expected: bool",
40+
"test_cases": "[([1, 2, 3, 1], True), ([1, 2, 3, 4], False), ([1, 1, 1, 3, 3, 4, 3, 2, 4, 2], True)]",
41+
"body": "result = self.solution.contains_duplicate(nums)\nassert result == expected"
42+
}
43+
],
44+
"playground_imports": "from solution import Solution",
45+
"playground_test_case": "# Example test case\nnums = [1, 2, 3, 1]\nexpected = True",
46+
"playground_execution": "result = Solution().contains_duplicate(nums)\nresult",
47+
"playground_assertion": "assert result == expected"
48+
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PYTHON_VERSION = 3.13
2-
PROBLEM ?= middle_of_the_linked_list
2+
PROBLEM ?= contains_duplicate
33
FORCE ?= 0
44
COMMA := ,
55

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Contains Duplicate
2+
3+
**Difficulty:** Easy
4+
**Topics:** Array, Hash Table, Sorting
5+
**Tags:** grind-75
6+
7+
**LeetCode:** [Problem 217](https://leetcode.com/problems/contains-duplicate/description/)
8+
9+
## Problem Description
10+
11+
Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.
12+
13+
## Examples
14+
15+
### Example 1:
16+
17+
```
18+
Input: nums = [1,2,3,1]
19+
Output: true
20+
```
21+
22+
**Explanation:** The element 1 occurs at the indices 0 and 3.
23+
24+
### Example 2:
25+
26+
```
27+
Input: nums = [1,2,3,4]
28+
Output: false
29+
```
30+
31+
**Explanation:** All elements are distinct.
32+
33+
### Example 3:
34+
35+
```
36+
Input: nums = [1,1,1,3,3,4,3,2,4,2]
37+
Output: true
38+
```
39+
40+
## Constraints
41+
42+
- 1 <= nums.length <= 10^5
43+
- -10^9 <= nums[i] <= 10^9

leetcode/contains_duplicate/__init__.py

Whitespace-only changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "imports",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": ["from solution import Solution"]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": null,
14+
"id": "setup",
15+
"metadata": {},
16+
"outputs": [],
17+
"source": ["# Example test case\nnums = [1, 2, 3, 1]\nexpected = True"]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": null,
22+
"id": "execute",
23+
"metadata": {},
24+
"outputs": [],
25+
"source": ["result = Solution().contains_duplicate(nums)\nresult"]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"id": "test",
31+
"metadata": {},
32+
"outputs": [],
33+
"source": ["assert result == expected"]
34+
}
35+
],
36+
"metadata": {
37+
"kernelspec": {
38+
"display_name": "leetcode-py-py3.13",
39+
"language": "python",
40+
"name": "python3"
41+
},
42+
"language_info": {
43+
"codemirror_mode": {
44+
"name": "ipython",
45+
"version": 3
46+
},
47+
"file_extension": ".py",
48+
"mimetype": "text/x-python",
49+
"name": "python",
50+
"nbconvert_exporter": "python3",
51+
"pygments_lexer": "ipython3",
52+
"version": "3.13.7"
53+
}
54+
},
55+
"nbformat": 4,
56+
"nbformat_minor": 5
57+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
# Time: O(n)
3+
# Space: O(n)
4+
def contains_duplicate(self, nums: list[int]) -> bool:
5+
seen = set()
6+
for num in nums:
7+
if num in seen:
8+
return True
9+
seen.add(num)
10+
return False
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pytest
2+
3+
from leetcode_py.test_utils import logged_test
4+
5+
from .solution import Solution
6+
7+
8+
class TestContainsDuplicate:
9+
def setup_method(self):
10+
self.solution = Solution()
11+
12+
@pytest.mark.parametrize(
13+
"nums, expected",
14+
[
15+
([1, 2, 3, 1], True),
16+
([1, 2, 3, 4], False),
17+
([1, 1, 1, 3, 3, 4, 3, 2, 4, 2], True),
18+
([], False),
19+
([1], False),
20+
([1, 1], True),
21+
([-1, -2, -3, -1], True),
22+
([-1, -2, -3, -4], False),
23+
([0, 0], True),
24+
([1000000, 999999, 1000000], True),
25+
(list(range(1000)), False),
26+
([1] * 1000, True),
27+
],
28+
)
29+
@logged_test
30+
def test_contains_duplicate(self, nums: list[int], expected: bool):
31+
result = self.solution.contains_duplicate(nums)
32+
assert result == expected

0 commit comments

Comments
 (0)