Skip to content

Commit 4a1cf71

Browse files
committed
Adding Simple Solution - Problem - 219 - Contains Duplicate II
1 parent 513d961 commit 4a1cf71

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 219
6+
## Problem Name: Contains Duplicate II
7+
##===================================
8+
#
9+
#Given an array of integers and an integer k,
10+
#find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and
11+
#the absolute difference between i and j is at most k.
12+
#
13+
#Example 1:
14+
#
15+
#Input: nums = [1,2,3,1], k = 3
16+
#Output: true
17+
#Example 2:
18+
#
19+
#Input: nums = [1,0,1,1], k = 1
20+
#Output: true
21+
#Example 3:
22+
#
23+
#Input: nums = [1,2,3,1,2,3], k = 2
24+
#Output: false
25+
class Solution:
26+
def containsNearbyDuplicate(self, nums, k):
27+
tmp = {} #Initialize tmp dictionary
28+
for i in range(len(nums)): #Loop through numbers
29+
if nums[i] in tmp and i - tmp[nums[i]] <= k: #Condition-check: If number in dictionary and absolute difference is at most k
30+
return True #We return true
31+
tmp[nums[i]] = i #Update dictionary
32+
return False #Return False

0 commit comments

Comments
 (0)