Anything wrong with my code?


  • 0
    T
    public int removeDuplicates(int[] A) {
    
            Set<Integer> set1 = new HashSet<Integer>();
    
            for (int i =0;i<A.length;i++){
                set1.add(new Integer(A[i]));
            }
      
            int[] result = new int[set1.size()];
            int i =0;
            for( Integer s : set1 ) {
            result[i++] = s; 
            }
            
            return result.length;
        }
    

    Its showed that the input was [1,1,2]
    it said my output was [1,1], but the expected out put should be[1,2].
    I ran the code in eclipse, it went well and the output was [1,2]. What is going on here?


  • 0
    S

    Your code does successfully create another array 'result' which stores all the unique values, but the interface does not allow you to return it; thus the result is lost when the function terminates. Instead, you should remove duplicated elements 'in-place' (directly modifying A rather than creating another array) as is explicitly required.


  • 0
    T

    Thanks for replying!


Log in to reply
 

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