算法 Java Leetcode

Find the single integer.

Question

Given an array of integers, every element appears twice except for one. Find that single one.
See it on Leetcode

1
Example: Given int[] arr = {1, 2, 1, 3, 4, 3, 2}, return 4.

Hint

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Solution

  • java
  • cpp
1
2
3
4
5
6
7
8
9
public class Solution {
public int singleNumber(int[] nums) {
int result = 0;
for (int i:nums){
result ^= i;
}
return result;
}
}