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

来源:互联网 发布:office2016破解软件 编辑:程序博客网 时间:2024/05/01 06:37

方法一:

import java.util.Arrays;

public class Solution {
    public int singleNumber(int[] A) {
        Arrays.sort(A);
        int i=0;
        for(i=0;i<A.length;i++){
            if(i+1<A.length&&A[i]==A[i+1])
                i=i+1;
            else
                break;
        }
         return A[i];
    }

}




方法二:

import java.util.Arrays;
public class Solution {
    public int singleNumber(int[] A) {
       int a=0;
       for(int i=0;i<A.length;i++)
            a=a^A[i];
        return a;
    }
}

0 0