↓Skip to main content
  1. LeetCode/

LeetCode 219: Contains Duplicate II

·2 mins· ·
LeetCode Easy Array Hash-Table Sliding-Window
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-21

  • Submissions: Wrong Answer → Runtime Error → Wrong Answer ×2 → Accepted ×4

Approach
#

A hash map keeps the last index of each value (Hash Table). At nums[i], compare only with its previous occurrence: if the nearest one is farther than k, earlier ones are farther still. Equivalently, check for a duplicate inside a fixed window of length k + 1 (Sliding Window Template 1).

My Attempts
#

The first four versions stored every index of each value in a list and then compared:

  • Attempt 1 built the map and returned nothing.
  • Attempt 2 wrote d.values without parentheses (TypeError).
  • Attempt 3 compared the first and the last occurrences, and a value seen once had distance 0, so it counted as a duplicate: [3], k = 2 returned True.
  • Attempt 4 excluded single occurrences but still compared the ends: in [0, 0, 0] with k = 1, adjacent zeros are 1 apart so the answer is True, but the ends are 2 apart and it returned False.

Keeping only the last index passed.

Solution
#

class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
        last = {}
        for i, v in enumerate(nums):
            if v in last and i - last[v] <= k:
                return True
            last[v] = i
        return False

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Compare consecutive occurrences, not the first and the last.
  • A value that appears once is not a duplicate.

Interview Explanation
#

A hash map stores each value’s previous index; on a repeat I check whether the distance is at most k and then update it. Only the nearest occurrence matters since earlier ones are farther. Time O(n), space O(n).

Related

LeetCode 217: Contains Duplicate
·1 min
LeetCode Easy Array Hash-Table Sorting
LeetCode 217 note: if converting to a set shortens the array, there is a duplicate
LeetCode 169: Majority Element
·2 mins
LeetCode Easy Array Hash-Table Divide-and-Conquer Sorting
LeetCode 169 note: Boyer-Moore voting, where different values cancel in pairs and the survivor is the one appearing more than half the time
LeetCode 1: Two Sum
·2 mins
LeetCode Easy Array Hash-Table
LeetCode 1 note: store seen numbers in a hash map while scanning and look up target - x; checking before storing prevents using one element twice
LeetCode 202: Happy Number
·1 min
LeetCode Easy Hash-Table Math Two-Pointers Floyds-Cycle-Finding-Algorithm
LeetCode 202 note: repeatedly take the sum of squared digits, which reaches 1 or enters a cycle; detect it with a set of seen numbers or fast and slow pointers
LeetCode 205: Isomorphic Strings
·2 mins
LeetCode Easy Hash-Table String
LeetCode 205 note: characters must map one to one; it holds when the distinct characters and distinct pairs all have the same count
LeetCode 228: Summary Ranges
·2 mins
LeetCode Easy Array
LeetCode 228 note: scan once, extending the current range when a number continues the previous one and opening a new range otherwise, then format