A average solution but have a doubt


  • 0
    A

    I have written the following code which works fine:

    public int singleNumber(int[] a) {
         if(a.length==1)return a[0]; 
        Arrays.sort(a);
        boolean duplicate=false;
        boolean found=false;
        int r=Integer.MAX_VALUE;
        int i=0;
        for(i=0;i<a.length-1;i++){
        if(!duplicate && a[i]!=a[i+1]){
            found=true;
            r=a[i];
            break;
        }
        else if(a[i]!=a[i+1]){
            duplicate=false;
        }
        else if(a[i]==a[i+1]){
                duplicate=true;
                continue;
            }
        
        }
        if(found)
        return r;
        return a[i];
        
    }
    

    My question is in real interview does writing the xor program looks good. What interviewer will judge except that I am good at remembering solution. Does it apply for any question that we should use a normal solution and not a one line solution?


  • 0
    A

    Unless you managed to find an O(n) sorting algorithm, I don't think you've met the requirements:

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


Log in to reply
 

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