Logo

dev-resources.site

for different kinds of informations.

Leetcode โ€” 3289. The Two Sneaky Numbers of Digitville

Published at
1/2/2025
Categories
leetcode
arrays
java
numbers
Author
bendlmp
Categories
4 categories in total
leetcode
open
arrays
open
java
open
numbers
open
Author
7 person written this
bendlmp
open
Leetcode โ€” 3289. The Two Sneaky Numbers of Digitville

Itโ€™s an easy problem with description being:

In the town of Digitville, there was a list of numbers called nums containing integers from 0 to n - 1. Each number was supposed to appear exactly once in the list, however, two mischievous numbers sneaked in an additional time, making the list longer than usual.

As the town detective, your task is to find these two sneaky numbers. Return an array of size two containing the two numbers (in any order), so peace can return to Digitville.

Example 1:

Input: nums = [0,1,1,0]

Output: [0,1]

Explanation:

The numbers 0 and 1 each appear twice in the array.

Example 2:

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

Output: [2,3]

Explanation:

The numbers 2 and 3 each appear twice in the array.

Example 3:

Input: nums = [7,1,5,4,3,4,6,0,9,5,8,2]

Output: [4,5]

Explanation:

The numbers 4 and 5 each appear twice in the array.

Constraints:

2 <= n <= 100

nums.length == n + 2

0 <= nums[i] < n

The input is generated such that nums contains exactly two repeated elements.

This problem has many ways to get it sorted out, you could use a set, a map, array as map, even use bits, but since itโ€™s an easy problem perhaps we shouldnโ€™t go far deep into the rabbit hole.

For the solution I went with simple approach that would be sorting it out the array and then on an iteration check if the previous number is equal, if yes, add into my result and thatโ€™s it:

class Solution {
    public int[] getSneakyNumbers(int[] nums) {

        // build the response and the pivot for the first item of the response array
        int pivot = 0;
        int[] response = new int[2];

        // sort nums array to make it easy to identify duplication
        Arrays.sort(nums);

        // iterate and numbers nearby are the ones considered sneaky, grab them and add into the response
        for(int i=1;i<nums.length;i++) {
            if(nums[i-1]==nums[i]){
                response[pivot] = nums[i];
                pivot++;
            }
        }

        // return response
        return response;
    } 
}
Enter fullscreen mode Exit fullscreen mode

Runtime: 2 ms, faster than 70.75% of Java online submissions.

Memory Usage: 44.49 MB, less than 86.86% of Java online submissions.

If you are sure that there will be no more than two you could do extra check, but besides that this solution fulfills most of itโ€™s needs.

โ€”

Thatโ€™s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.

Until next post! :)

leetcode Article's
30 articles in total
Favicon
Neetcode Roadmap Part 1
Favicon
2429. Minimize XOR
Favicon
A tรฉcnica dos dois ponteiros
Favicon
2657. Find the Prefix Common Array of Two Arrays
Favicon
Time Complexity, Big-O for Beginners
Favicon
LeetCode Challenge: 383. Ransom Note - JavaScript Solution ๐Ÿš€
Favicon
3223. Minimum Length of String After Operations
Favicon
Leet code
Favicon
2116. Check if a Parentheses String Can Be Valid
Favicon
LeetCode Challenge: 73. Set Matrix Zeroes - JavaScript Solution ๐Ÿš€
Favicon
LeetCode Challenge: 290. Word Pattern - JavaScript Solution ๐Ÿš€
Favicon
LeetCode Challenge: 205. Isomorphic Strings - JavaScript Solution ๐Ÿš€
Favicon
Leetcode: 73 Set Matrix Zeroes
Favicon
LeetCode Challenge: 36.Valid Sudoku - JavaScript Solution ๐Ÿš€
Favicon
Count prefix and suffix I and II
Favicon
Leetcode Blind 75
Favicon
Rabin Karp (hashing) String pattern matching
Favicon
Leetcode โ€” 2942. Find Words Containing Character
Favicon
Automating Your LeetCode Journey: Building an Enterprise-Grade LeetCode to GitHub Sync System
Favicon
Understanding the XOR Operator: A Powerful Tool in Computing
Favicon
Kadane's Algorithm: Leetcode 53 Maximum subarray
Favicon
1768. Merge Strings Alternately
Favicon
Find all anagrams in the string[Fixed Window pattern]
Favicon
No of ways to split Array
Favicon
Leetcode โ€” 3289. The Two Sneaky Numbers of Digitville
Favicon
Range sum query 2D - Immutable
Favicon
Range Sum Query - Immutable
Favicon
Count vowel strings in ranges
Favicon
Yay! Reached 1035+ days Daily Coding Streak on Leetcode!
Favicon
Leetcode 75. Sort Colors

Featured ones: