Skip to content

Commit 0265718

Browse files
committed
[First week][contains-duplicate] adds answer
1 parent 8901b1d commit 0265718

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

contains-duplicate/routiful.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# FIRST WEEK
2+
3+
# Question : 217. Contains Duplicate
4+
# 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.
5+
6+
# Example 1:
7+
# Input: nums = [1,2,3,1]
8+
# Output: true
9+
# Explanation:
10+
# The element 1 occurs at the indices 0 and 3.
11+
12+
# Example 2:
13+
# Input: nums = [1,2,3,4]
14+
# Output: false
15+
# Explanation:
16+
# All elements are distinct.
17+
18+
# Example 3:
19+
# Input: nums = [1,1,1,3,3,4,3,2,4,2]
20+
# Output: true
21+
22+
# Constraints:
23+
# 1 <= nums.length <= 105
24+
# -109 <= nums[i] <= 109
25+
26+
# Notes:
27+
# Counts every elements(in the list) using 'count()' API. Luckily it is less than O(n) but maximum O(n)
28+
# Makes Dict; the key is elements of the list. If the length of them are different, the list has duplicate elements. It is O(n)
29+
30+
class Solution:
31+
def containsDuplicate(self, nums: List[int]) -> bool:
32+
d = {}
33+
for n in nums:
34+
d[n] = 1
35+
if len(d) != len(nums):
36+
return True
37+
return False

0 commit comments

Comments
 (0)