Leetcode: Remove duplicates from sorted array (Javascript)












0















Why does my solution work in the console but not on leetcode?



var removeDuplicates = function(nums) {
let res = ;
for(let num of nums) {
if(res.includes(num) === false) {
res.push(num);
}
}
return res.length;
};


Console:
screenshot



Leetcode:



let arr = [1, 1, 2]

removeDuplicates(arr) // 3









share|improve this question

























  • have you tried using If(res.indexOf(num) < 0) ? instead of if(res.includes(num) === false)

    – Venkatesh Konatham
    Nov 22 '18 at 7:01
















0















Why does my solution work in the console but not on leetcode?



var removeDuplicates = function(nums) {
let res = ;
for(let num of nums) {
if(res.includes(num) === false) {
res.push(num);
}
}
return res.length;
};


Console:
screenshot



Leetcode:



let arr = [1, 1, 2]

removeDuplicates(arr) // 3









share|improve this question

























  • have you tried using If(res.indexOf(num) < 0) ? instead of if(res.includes(num) === false)

    – Venkatesh Konatham
    Nov 22 '18 at 7:01














0












0








0


1






Why does my solution work in the console but not on leetcode?



var removeDuplicates = function(nums) {
let res = ;
for(let num of nums) {
if(res.includes(num) === false) {
res.push(num);
}
}
return res.length;
};


Console:
screenshot



Leetcode:



let arr = [1, 1, 2]

removeDuplicates(arr) // 3









share|improve this question
















Why does my solution work in the console but not on leetcode?



var removeDuplicates = function(nums) {
let res = ;
for(let num of nums) {
if(res.includes(num) === false) {
res.push(num);
}
}
return res.length;
};


Console:
screenshot



Leetcode:



let arr = [1, 1, 2]

removeDuplicates(arr) // 3






javascript algorithm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 7:03







annieg4123

















asked Nov 22 '18 at 6:58









annieg4123annieg4123

915




915













  • have you tried using If(res.indexOf(num) < 0) ? instead of if(res.includes(num) === false)

    – Venkatesh Konatham
    Nov 22 '18 at 7:01



















  • have you tried using If(res.indexOf(num) < 0) ? instead of if(res.includes(num) === false)

    – Venkatesh Konatham
    Nov 22 '18 at 7:01

















have you tried using If(res.indexOf(num) < 0) ? instead of if(res.includes(num) === false)

– Venkatesh Konatham
Nov 22 '18 at 7:01





have you tried using If(res.indexOf(num) < 0) ? instead of if(res.includes(num) === false)

– Venkatesh Konatham
Nov 22 '18 at 7:01












2 Answers
2






active

oldest

votes


















1














You can try changing includes to indexOf, may be includes is not working in your environment. Also, instead of returning length you should return res.



Just in case you want to try another approach, you can look at Sets like below






var removeDuplicates = function(nums) {
return [...new Set(nums)]
};

console.log(removeDuplicates([1,1,2]))

console.log(removeDuplicates([1,1,2,3]))








share|improve this answer





















  • 1





    No need to do array#reduce()...

    – Yosvel Quintero
    Nov 22 '18 at 7:05











  • Yes @YosvelQuintero just realised that. :)

    – Nitish Narang
    Nov 22 '18 at 7:06






  • 1





    Sets can take arrays of inputs directly

    – Nitish Narang
    Nov 22 '18 at 7:06






  • 1





    Good to help.. Removing my answer because yours was first

    – Yosvel Quintero
    Nov 22 '18 at 7:09








  • 1





    That's so kind of you @YosvelQuintero. That's why I love SO, people are so willing to promote fellow learners. Thank you :)

    – Nitish Narang
    Nov 22 '18 at 7:10



















0














You don't use sortness properly. Algorithmically it is more effective to compare item with previous one, so complexity is O(N).



Perhaps JS has some high-order function like Python groupby to make shorter code, but described method is definitely the best possible from algorithmical point of view.



ideone



var removeDuplicates = function(nums) {
let res = ;
let last = NaN
for(i=0; i<nums.length; i++) {
if(nums[i] != last) {
res.push(nums[i]);
last = nums[i];
}
}
return res.length;
};

let arr = [1, 1, 2]
print(removeDuplicates(arr))

>>2





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425420%2fleetcode-remove-duplicates-from-sorted-array-javascript%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can try changing includes to indexOf, may be includes is not working in your environment. Also, instead of returning length you should return res.



    Just in case you want to try another approach, you can look at Sets like below






    var removeDuplicates = function(nums) {
    return [...new Set(nums)]
    };

    console.log(removeDuplicates([1,1,2]))

    console.log(removeDuplicates([1,1,2,3]))








    share|improve this answer





















    • 1





      No need to do array#reduce()...

      – Yosvel Quintero
      Nov 22 '18 at 7:05











    • Yes @YosvelQuintero just realised that. :)

      – Nitish Narang
      Nov 22 '18 at 7:06






    • 1





      Sets can take arrays of inputs directly

      – Nitish Narang
      Nov 22 '18 at 7:06






    • 1





      Good to help.. Removing my answer because yours was first

      – Yosvel Quintero
      Nov 22 '18 at 7:09








    • 1





      That's so kind of you @YosvelQuintero. That's why I love SO, people are so willing to promote fellow learners. Thank you :)

      – Nitish Narang
      Nov 22 '18 at 7:10
















    1














    You can try changing includes to indexOf, may be includes is not working in your environment. Also, instead of returning length you should return res.



    Just in case you want to try another approach, you can look at Sets like below






    var removeDuplicates = function(nums) {
    return [...new Set(nums)]
    };

    console.log(removeDuplicates([1,1,2]))

    console.log(removeDuplicates([1,1,2,3]))








    share|improve this answer





















    • 1





      No need to do array#reduce()...

      – Yosvel Quintero
      Nov 22 '18 at 7:05











    • Yes @YosvelQuintero just realised that. :)

      – Nitish Narang
      Nov 22 '18 at 7:06






    • 1





      Sets can take arrays of inputs directly

      – Nitish Narang
      Nov 22 '18 at 7:06






    • 1





      Good to help.. Removing my answer because yours was first

      – Yosvel Quintero
      Nov 22 '18 at 7:09








    • 1





      That's so kind of you @YosvelQuintero. That's why I love SO, people are so willing to promote fellow learners. Thank you :)

      – Nitish Narang
      Nov 22 '18 at 7:10














    1












    1








    1







    You can try changing includes to indexOf, may be includes is not working in your environment. Also, instead of returning length you should return res.



    Just in case you want to try another approach, you can look at Sets like below






    var removeDuplicates = function(nums) {
    return [...new Set(nums)]
    };

    console.log(removeDuplicates([1,1,2]))

    console.log(removeDuplicates([1,1,2,3]))








    share|improve this answer















    You can try changing includes to indexOf, may be includes is not working in your environment. Also, instead of returning length you should return res.



    Just in case you want to try another approach, you can look at Sets like below






    var removeDuplicates = function(nums) {
    return [...new Set(nums)]
    };

    console.log(removeDuplicates([1,1,2]))

    console.log(removeDuplicates([1,1,2,3]))








    var removeDuplicates = function(nums) {
    return [...new Set(nums)]
    };

    console.log(removeDuplicates([1,1,2]))

    console.log(removeDuplicates([1,1,2,3]))





    var removeDuplicates = function(nums) {
    return [...new Set(nums)]
    };

    console.log(removeDuplicates([1,1,2]))

    console.log(removeDuplicates([1,1,2,3]))






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 '18 at 7:07

























    answered Nov 22 '18 at 7:04









    Nitish NarangNitish Narang

    2,948815




    2,948815








    • 1





      No need to do array#reduce()...

      – Yosvel Quintero
      Nov 22 '18 at 7:05











    • Yes @YosvelQuintero just realised that. :)

      – Nitish Narang
      Nov 22 '18 at 7:06






    • 1





      Sets can take arrays of inputs directly

      – Nitish Narang
      Nov 22 '18 at 7:06






    • 1





      Good to help.. Removing my answer because yours was first

      – Yosvel Quintero
      Nov 22 '18 at 7:09








    • 1





      That's so kind of you @YosvelQuintero. That's why I love SO, people are so willing to promote fellow learners. Thank you :)

      – Nitish Narang
      Nov 22 '18 at 7:10














    • 1





      No need to do array#reduce()...

      – Yosvel Quintero
      Nov 22 '18 at 7:05











    • Yes @YosvelQuintero just realised that. :)

      – Nitish Narang
      Nov 22 '18 at 7:06






    • 1





      Sets can take arrays of inputs directly

      – Nitish Narang
      Nov 22 '18 at 7:06






    • 1





      Good to help.. Removing my answer because yours was first

      – Yosvel Quintero
      Nov 22 '18 at 7:09








    • 1





      That's so kind of you @YosvelQuintero. That's why I love SO, people are so willing to promote fellow learners. Thank you :)

      – Nitish Narang
      Nov 22 '18 at 7:10








    1




    1





    No need to do array#reduce()...

    – Yosvel Quintero
    Nov 22 '18 at 7:05





    No need to do array#reduce()...

    – Yosvel Quintero
    Nov 22 '18 at 7:05













    Yes @YosvelQuintero just realised that. :)

    – Nitish Narang
    Nov 22 '18 at 7:06





    Yes @YosvelQuintero just realised that. :)

    – Nitish Narang
    Nov 22 '18 at 7:06




    1




    1





    Sets can take arrays of inputs directly

    – Nitish Narang
    Nov 22 '18 at 7:06





    Sets can take arrays of inputs directly

    – Nitish Narang
    Nov 22 '18 at 7:06




    1




    1





    Good to help.. Removing my answer because yours was first

    – Yosvel Quintero
    Nov 22 '18 at 7:09







    Good to help.. Removing my answer because yours was first

    – Yosvel Quintero
    Nov 22 '18 at 7:09






    1




    1





    That's so kind of you @YosvelQuintero. That's why I love SO, people are so willing to promote fellow learners. Thank you :)

    – Nitish Narang
    Nov 22 '18 at 7:10





    That's so kind of you @YosvelQuintero. That's why I love SO, people are so willing to promote fellow learners. Thank you :)

    – Nitish Narang
    Nov 22 '18 at 7:10













    0














    You don't use sortness properly. Algorithmically it is more effective to compare item with previous one, so complexity is O(N).



    Perhaps JS has some high-order function like Python groupby to make shorter code, but described method is definitely the best possible from algorithmical point of view.



    ideone



    var removeDuplicates = function(nums) {
    let res = ;
    let last = NaN
    for(i=0; i<nums.length; i++) {
    if(nums[i] != last) {
    res.push(nums[i]);
    last = nums[i];
    }
    }
    return res.length;
    };

    let arr = [1, 1, 2]
    print(removeDuplicates(arr))

    >>2





    share|improve this answer






























      0














      You don't use sortness properly. Algorithmically it is more effective to compare item with previous one, so complexity is O(N).



      Perhaps JS has some high-order function like Python groupby to make shorter code, but described method is definitely the best possible from algorithmical point of view.



      ideone



      var removeDuplicates = function(nums) {
      let res = ;
      let last = NaN
      for(i=0; i<nums.length; i++) {
      if(nums[i] != last) {
      res.push(nums[i]);
      last = nums[i];
      }
      }
      return res.length;
      };

      let arr = [1, 1, 2]
      print(removeDuplicates(arr))

      >>2





      share|improve this answer




























        0












        0








        0







        You don't use sortness properly. Algorithmically it is more effective to compare item with previous one, so complexity is O(N).



        Perhaps JS has some high-order function like Python groupby to make shorter code, but described method is definitely the best possible from algorithmical point of view.



        ideone



        var removeDuplicates = function(nums) {
        let res = ;
        let last = NaN
        for(i=0; i<nums.length; i++) {
        if(nums[i] != last) {
        res.push(nums[i]);
        last = nums[i];
        }
        }
        return res.length;
        };

        let arr = [1, 1, 2]
        print(removeDuplicates(arr))

        >>2





        share|improve this answer















        You don't use sortness properly. Algorithmically it is more effective to compare item with previous one, so complexity is O(N).



        Perhaps JS has some high-order function like Python groupby to make shorter code, but described method is definitely the best possible from algorithmical point of view.



        ideone



        var removeDuplicates = function(nums) {
        let res = ;
        let last = NaN
        for(i=0; i<nums.length; i++) {
        if(nums[i] != last) {
        res.push(nums[i]);
        last = nums[i];
        }
        }
        return res.length;
        };

        let arr = [1, 1, 2]
        print(removeDuplicates(arr))

        >>2






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 8:05

























        answered Nov 22 '18 at 7:14









        MBoMBo

        47.3k22949




        47.3k22949






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425420%2fleetcode-remove-duplicates-from-sorted-array-javascript%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'