My Java Code, use additional array


  • 0
    A

    Use an integer array to keep trace of the number of occurrences of '1' at every corresponding bit.

    public int singleNumber(int[] A) {
        int[] sum_for_bits = new int[32];
        for(int i = 0; i < 32; i++){
            sum_for_bits[i] = 0;
        }
        
        for(int i = 0; i < A.length; i++){
            for(int j = 0; j <32; j++){
                if((A[i] & (1<<j)) != 0){
                    sum_for_bits[j]++;
                }
            }
        }
        
        int singlN = 0;
        for(int i = 0; i < 32; i++){
            if(sum_for_bits[i] % 3 != 0){
                singlN |= (1<<i);
            }
        }
        return singlN;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.