利用HashSet
import java.util.*; public class Solution { public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) { Set<Integer> set = new HashSet<Integer>(); for (int i = 0; i < array.length; i++) { if (!set.contains(array[i])) set.add(array[i]); else set.remove(array[i]); } //取值方法1 int k = 0; for (Integer i : set) { if(k++ == 0) num1[0] = i; else num2[0] = i; } //取值方法2 Integer [] in = new Integer [2]; set.toArray(in); num1[0] = in[0]; num2[0] = in[1]; } }