Advertisement
3289. The Two Sneaky Numbers of Digitville
MediumView on LeetCode
Time: O(n)
Space: O(n)
Approach
Find duplicate by tracking seen values; repeated and missing follow from XOR or count.
3289.cs
C#
// Approach: Find duplicate by tracking seen values; repeated and missing follow from XOR or count.
// Time: O(n) Space: O(n)
public class Solution
{
public int[] GetSneakyNumbers(int[] nums)
{
// Array to store the result (two duplicate numbers)
int[] result = new int[2];
// Frequency counter array (assuming numbers range from 0 to 99)
int[] frequencyCounter = new int[100];
// Index to track position in result array
int resultIndex = 0;
// Iterate through each number in the input array
foreach (int currentNumber in nums)
{
// Increment the frequency count for current number
frequencyCounter[currentNumber]++;
// If this number appears for the second time, add it to result
if (frequencyCounter[currentNumber] == 2)
{
result[resultIndex] = currentNumber;
resultIndex++;
}
}
return result;
}
}Advertisement
Was this solution helpful?