Back to posts

LeetCode Challenge Day 46 β€” 3289. Get Sneaky Numbers

Nitin Ahirwal / October 31, 2025

LeetCode ChallengeDay 46ArrayHashingEasyJavaScriptPortfolio

Hey folks πŸ‘‹

This is Day 46 of my LeetCode streak πŸš€
Today’s problem is 3289. Get Sneaky Numbers β€” an easy array problem that involves finding two numbers that appear twice in the given array.


πŸ“Œ Problem Statement

You are given an integer array nums of length n + 2 where every integer between 0 and n - 1 appears exactly once, except for two numbers that appear twice.

Return an array containing the two duplicated numbers.

Example 1:

Input: nums = [0, 1, 2, 3, 2, 5, 4, 5]  
Output: [2, 5]

Example 2:

Input: nums = [0, 1, 2, 3, 0, 4, 5, 6]  
Output: [0, 3]

πŸ’‘ Intuition

Since all numbers except two are unique,
we just need to detect which numbers appear twice.

A simple and efficient way to do this is to use a seen array or set β€” mark each number the first time it appears, and if it appears again, add it to the result.


πŸ”‘ Approach

  1. Initialize a boolean array seen of size n to track visited numbers.
  2. Iterate through each element in nums.
    • If a number is already marked in seen, it’s a duplicate β†’ add to result.
    • Otherwise, mark it as seen.
  3. Stop once both duplicates are found.

⏱️ Complexity Analysis

  • Time complexity:
    (O(n)) β†’ Single pass through the array.

  • Space complexity:
    (O(n)) β†’ Extra space for the seen array.


πŸ§‘β€πŸ’» Code (JavaScript)

/**
 * @param {number[]} nums
 * @return {number[]}
 */
var getSneakyNumbers = function(nums) {
    const n = nums.length - 2;     // since two numbers are duplicated
    const seen = new Array(n).fill(false);
    const res = [];

    for (let num of nums) {
        if (seen[num]) {
            res.push(num);
            if (res.length === 2) break; // found both sneaky numbers
        } else {
            seen[num] = true;
        }
    }

    return res;
};

πŸ§ͺ Example Walkthrough

Input:
nums = [0, 1, 2, 3, 2, 5, 4, 5]

Steps:

  • Seen = []

  • 0 β†’ mark seen

  • 1 β†’ mark seen

  • 2 β†’ mark seen

  • 3 β†’ mark seen

  • 2 β†’ already seen β†’ add to result

  • 5 β†’ mark seen

  • 4 β†’ mark seen

  • 5 β†’ already seen β†’ add to result

βœ… Output = [2, 5]


🎯 Reflection

A nice warm-up problem to strengthen your array manipulation and hashing intuition.
Simple yet satisfying β€” a perfect reminder that clean thinking leads to clean code ✨

That’s it for Day 46 of my LeetCode journey!
See you tomorrow for Day 47 πŸ”₯

Happy Coding πŸ‘¨β€πŸ’»