public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
Arrays.sort(array);
Stack<Integer> stack = new Stack<>();
for (int i = 0;i<array.length;i++) {
if (stack.empty() || stack.peek()!=array[i]) {
stack.push(array[i]);
}else if (stack.peek() == array[i]) {
stack.pop();
}
}
num1[0] = stack.pop();
num2[0] = stack.pop();
}
我们先排序数组,这样相同的数字就会在一起,然后遍历判断每一个是否和栈顶元素相同,相同就让他出栈,否则就进栈,最后剩下的元素一定会是只出现一次的两个数。