''' LastEditTime: 2025-10-31 22:21:51 ''' from typing importList
classSolution: defgetSneakyNumbers(self, nums: List[int]) -> List[int]: se = set() ans = [] for t in nums: if t in se: ans.append(t) else: se.add(t) return ans
classSolution { publicint[] getSneakyNumbers(int[] nums) { int[] ans = newint[2]; intalready=0; Set<Integer> se = newHashSet<>(); for (int t : nums) { if (se.contains(t)) { ans[already++] = t; } else { se.add(t); } } return ans; } }
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* * @LastEditTime: 2025-10-31 22:24:27 */ package main
funcgetSneakyNumbers(nums []int) (ans []int) { se := map[int]struct{}{} for _, t := range nums { if _, ok := se[t]; ok { ans = append(ans, t) } else { se[t] = struct{}{} } } return }
Rust
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* * @LastEditTime: 2025-10-31 22:39:35 */ use std::collections::HashSet;
implSolution { pubfnget_sneaky_numbers(nums: Vec<i32>) ->Vec<i32> { letmut se = HashSet::new(); letmut ans = Vec::with_capacity(2); fortin nums { if se.contains(&t) { ans.push(t); } else { se.insert(t); } } ans } }