SingleNumberIIISolution { static int[] singleNumber(int[] nums) { if (nums == null || nums.length == 1) return nums; int sum = 0; for (int num : nums) { sum ^= num; } sum &= -sum; int[] result = {0, 0}; for (int num : nums) { if ((num & sum) == 0) { result[0] ^= num; } else { result[1] ^= num; } } return result; }    }
@Test public void singleNumber() { { int[] nums = {1, 2, 1, 3, 2, 5}; nums = SingleNumberIIISolution.singleNumber(nums); int[] result = {5, 3}; for (int i = 0; i < nums.length; i++) { assertEquals(result[i], nums[i]); } } { int[] nums = {1, 2, 1, 3, 2, 5, 5, 7}; nums = SingleNumberIIISolution.singleNumber(nums); int[] result = {3, 7}; for (int i = 0; i < nums.length; i++) { assertEquals(result[i], nums[i]); } } { int[] nums = {1, 2, 1, 3, 2, 5, 5, 7, 7, 9}; nums = SingleNumberIIISolution.singleNumber(nums); int[] result = {9, 3}; for (int i = 0; i < nums.length; i++) { assertEquals(result[i], nums[i]); } } }