The Two Sneaky Numbers of Digitville
Source: leetcode
Difficulty: Easy
Tags: array, hash-table, math

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.




C++ solution:
class Solution {
public:
    vector<int> getSneakyNumbers(vector<int>& a) {
        int n= a.size();
        vector<bool> cnt(101);
        vector<int> res;
        for(auto&x:a)
        {
            if(cnt[x]) res.push_back(x);
            cnt[x]=true;
        }
        return res;
    }
};

Go solution:
func getSneakyNumbers(nums []int) []int {
	seen := make(map[int]bool)
	var result []int

	for _, valor := range nums {
		if !seen[valor] {
			seen[valor] = true
		} else {
			result = append(result, valor)
		}
	}

	return result
}

Java solution:
class Solution {
    public int[] getSneakyNumbers(int[] nums) {
        int n = nums.length;
        int[] res = new int[2];
        HashSet<Integer> set = new HashSet<>();
        for(int i=0,j=0;i<n;i++){
            if(!set.isEmpty() && set.contains(nums[i])){
                res[j] = nums[i];
                j++;
            }
            else{
                set.add(nums[i]);
            }
        }
        return res;
    }
}

JavaScript solution:
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var getSneakyNumbers = function(nums) {
    let sortedArr = nums.sort((a,b) => a-b)
    let sneakyNumArr = [];
    for (let num = 0; num < sortedArr.length; num++) {
        if (sortedArr[num] == sortedArr[num+1]) {
            sneakyNumArr.push(sortedArr[num])
        }
    }
    return sneakyNumArr
};

Python solution:
def getSneakyNumbers(string: str) -> List[int]:
    chunk: List[str] = []
    numbers: Set[Tuple[str, ...]] = set()
    first: Optional[str] = None
    for i in range(1, len(string) - 1 if string[-1] == ']' else len(string) - 2):
        if string[i] != ',':
            chunk.append(string[i])
            continue
        _: Tuple[str, ...] = tuple(chunk)
        chunk = []
        if _ in numbers:
            if first is None:
                first = ''.join(_)
            else:
                return f'[{first},{"".join(_)}]'
        else:
            numbers.add(_)
    return f'[{first},{"".join(chunk)}]'

def main():    
    with open("user.out", "w") as f:
        f.write('\n'.join(getSneakyNumbers(_) for _ in sys.stdin) + '\n')

if __name__ == "__main__":
    main()
    sys.exit(0)

Ruby solution:
# @param {Integer[]} nums
# @return {Integer[]}
'''
'''
def get_sneaky_numbers(nums)
    nums.sort!()
    n = nums.size
    repeats = [-1,-1]
    repeatPtr = 0
    (0...n-1).each do |ptr|
        curVal = nums[ptr]
        nextVal = nums[ptr+1]
        if(curVal == nextVal && repeatPtr < 2)
            repeats[repeatPtr] = curVal
            repeatPtr += 1
        end
    end
    return repeats
end