Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
Example 1:
Input: nums = [1,2,3,1]
Output: true
Explanation:
The element 1 occurs at the indices 0 and 3.
Example 2:
Input: nums = [1,2,3,4]
Output: false
Explanation:
All elements are distinct.
Example 3:
Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true
Constraints:
1 <= nums.length <= 105-109 <= nums[i] <= 109Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2,3,1]
[1,2,3,4]
[1,1,1,3,3,4,3,2,4,2]
contains-duplicate-ii)contains-duplicate-iii)make-array-zero-by-subtracting-equal-amounts)find-valid-pair-of-adjacent-digits-in-string)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #217: Contains Duplicate
class Solution {
public boolean containsDuplicate(int[] nums) {
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; ++i) {
if (nums[i] == nums[i + 1]) {
return true;
}
}
return false;
}
}
// Accepted solution for LeetCode #217: Contains Duplicate
func containsDuplicate(nums []int) bool {
sort.Ints(nums)
for i, v := range nums[1:] {
if v == nums[i] {
return true
}
}
return false
}
# Accepted solution for LeetCode #217: Contains Duplicate
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return any(a == b for a, b in pairwise(sorted(nums)))
// Accepted solution for LeetCode #217: Contains Duplicate
impl Solution {
pub fn contains_duplicate(mut nums: Vec<i32>) -> bool {
nums.sort();
let n = nums.len();
for i in 1..n {
if nums[i - 1] == nums[i] {
return true;
}
}
false
}
}
// Accepted solution for LeetCode #217: Contains Duplicate
function containsDuplicate(nums: number[]): boolean {
nums.sort((a, b) => a - b);
const n = nums.length;
for (let i = 1; i < n; i++) {
if (nums[i - 1] === nums[i]) {
return true;
}
}
return false;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.