Share my O(n) time and O(1) space solution with duplicates are allowed at most n times

  • 2
    public class Solution {
    public int removeDuplicates(int[] nums) {
        if(nums==null || nums.length<3){
            return nums==null?0:nums.length;
        int dif = 2;
        for(int i=2;i<nums.length;i++){
        return dif;


    if N duplicates are allowed,you just need to change the nums[dif-2] to nums[dif-N] ,welcome to discuss with me .

Log in to reply

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