Why is this not working?


  • 0
    A
    public class Solution {
        public int removeDuplicates(int[] nums) {
            if(nums.length == 1){
                return 1;
            }
            int len = nums.length;
            int i = 0;
            int index = 0;
            while(i != nums.length){
                index = nums[i];
                i++;
                while(true){
                    if(i == nums.length){
                        break;
                    }
                    if(nums[i] == index){
                        len--;
                        i++;
                    }else{
                        i++;
                        break;
                    }
                }
                System.out.println(i);
            }
            return len;
        }
    }
    

    For
    input [1, 2, 2] it returns [1, 2, 2] instead of ```[1, 2]`` but when I hand run my algorithm it gives the proper length, which is 2?


Log in to reply
 

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