boolcontainsNearbyDuplicate(int* nums, int numsSize, int k){ structHashEntry *dictionary =NULL; for (int i = 0; i < numsSize; i++) { structHashEntry * pEntry = hashFindItem(&dictionary, nums[i]); if (NULL != pEntry && i - pEntry->val <= k) { hashFreeAll(&dictionary); returntrue; } hashAddItem(&dictionary, nums[i], i); } hashFreeAll(&dictionary); returnfalse; }
[sol1-Python3]
1 2 3 4 5 6 7 8
classSolution: defcontainsNearbyDuplicate(self, nums: List[int], k: int) -> bool: pos = {} for i, num inenumerate(nums): if num in pos and i - pos[num] <= k: returnTrue pos[num] = i returnFalse
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10
funccontainsNearbyDuplicate(nums []int, k int)bool { pos := map[int]int{} for i, num := range nums { if p, ok := pos[num]; ok && i-p <= k { returntrue } pos[num] = i } returnfalse }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12
var containsNearbyDuplicate = function(nums, k) { const map = newMap(); const length = nums.length; for (let i = 0; i < length; i++) { const num = nums[i]; if (map.has(num) && i - map.get(num) <= k) { returntrue; } map.set(num, i); } returnfalse; };
classSolution { publicbooleancontainsNearbyDuplicate(int[] nums, int k) { Set<Integer> set = newHashSet<Integer>(); intlength= nums.length; for (inti=0; i < length; i++) { if (i > k) { set.remove(nums[i - k - 1]); } if (!set.add(nums[i])) { returntrue; } } returnfalse; } }
[sol2-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
publicclassSolution { publicboolContainsNearbyDuplicate(int[] nums, int k) { ISet<int> set = new HashSet<int>(); int length = nums.Length; for (int i = 0; i < length; i++) { if (i > k) { set.Remove(nums[i - k - 1]); } if (!set.Add(nums[i])) { returntrue; } } returnfalse; } }
[sol2-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public: boolcontainsNearbyDuplicate(vector<int>& nums, int k){ unordered_set<int> s; int length = nums.size(); for (int i = 0; i < length; i++) { if (i > k) { s.erase(nums[i - k - 1]); } if (s.count(nums[i])) { returntrue; } s.emplace(nums[i]); } returnfalse; } };
boolcontainsNearbyDuplicate(int* nums, int numsSize, int k){ structHashEntry *cnt =NULL; for (int i = 0; i < numsSize; i++) { if (i > k) { hashEraseItem(&cnt, nums[i - k - 1]); } structHashEntry * pEntry = hashFindItem(&cnt, nums[i]); if (NULL != pEntry) { returntrue; } hashAddItem(&cnt, nums[i], 1); } hashFreeAll(&cnt); returnfalse; }
[sol2-Python3]
1 2 3 4 5 6 7 8 9 10
classSolution: defcontainsNearbyDuplicate(self, nums: List[int], k: int) -> bool: s = set() for i, num inenumerate(nums): if i > k: s.remove(nums[i - k - 1]) if num in s: returnTrue s.add(num) returnFalse
[sol2-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13
funccontainsNearbyDuplicate(nums []int, k int)bool { set := map[int]struct{}{} for i, num := range nums { if i > k { delete(set, nums[i-k-1]) } if _, ok := set[num]; ok { returntrue } set[num] = struct{}{} } returnfalse }
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
var containsNearbyDuplicate = function(nums, k) { const set = newSet(); const length = nums.length; for (let i = 0; i < length; i++) { if (i > k) { set.delete(nums[i - k - 1]); } if (set.has(nums[i])) { returntrue; } set.add(nums[i]) } returnfalse; };