Remove duplicates from array in Javascript | Algorithm Interview Question

Поділитися
Вставка
  • Опубліковано 8 вер 2024
  • How to Remove Duplicate values from an array of integers. Four solutions, 1 ) One brute-force method using a for loop, 2)sort and remove, 3) using JavaScript Objects and 4) Using ES6 / ES2015 Sets.

КОМЕНТАРІ • 365

  • @razt3757
    @razt3757 4 роки тому +71

    Quick note:
    Instead of declaring the length outside the for loop you can declare and initialize it at the beginning of the loop like this:
    for(let i=0,length=a.length;i

    •  4 роки тому +21

      Believe it or not, for (let i = 0; i < a.length; i++) is actually faster.
      Test it yourself using jsperf. There is no need to store the length of an array in a separate variable, the JS engine does it for you automatically. On Firefox, I get about 0.69% decrease in performance when I store the length in a a separate variable. Insignificant, but illustrates that there is no point in doing it.

    • @armanrozika
      @armanrozika 4 роки тому +5

      @ Good point if you are working in a browser environment, but I don't know if it's applicable in a nodejs environment tho

  • @tonycarbetta1497
    @tonycarbetta1497 6 років тому +83

    I failed this question on an interview and didn’t get the job because of it. Thank you for breaking it down so effectively.

    • @javascript_developer
      @javascript_developer 4 роки тому +9

      I got the job for the same question. I had watched this video already. and same question came to me.

    • @fitsumseyoum8611
      @fitsumseyoum8611 4 роки тому +3

      me too

    • @THWUFAILUZAMAN
      @THWUFAILUZAMAN 2 роки тому +1

      For me also asked the same question but I just converted array to a set and got the offer 😁

    • @abhishekbaghel1809
      @abhishekbaghel1809 2 роки тому

      I think we've all been there🥲

    • @archlinux8390
      @archlinux8390 2 роки тому

      Me too 🤣 2 times lost ,

  • @pratikpurohit3008
    @pratikpurohit3008 4 роки тому +5

    At 9:50 the Value inside are String Types. Object.keys(obj).map(Number) can be helpful. Thanks for your awesome videos and work!

  • @jacobc5655
    @jacobc5655 3 роки тому +2

    Great tutorial showing not just the "how" but also the so important "why"!

  • @Sephiga
    @Sephiga Рік тому

    DUDE you are a magician. I was stuck on TheOdinProject exercise 4, removeFromArray. I spent 3 days reading up on documentation understanding rest parameters, spread syntax, numerous array methods, making sure I was looping correctly and just was not understanding in the slightest why my solution wasn't working.
    Thanks for helping me understand better, i never would have thought of using indexOf with a strict equality to -1. That's a pretty neat trick. I'm so excited to learn more!
    Edit: Just checked their problem solution... wow i feel dumb 🤦

  • @gopalzalke2729
    @gopalzalke2729 3 роки тому +1

    This was first question in an interview and I didn't knew the best solution at that time. After coming back I learned about Object method.

  • @catscoffeecode
    @catscoffeecode 6 років тому +6

    cool video... I noticed two things... First sort: if in the array we have numbers that have more than one digit, in js, sort will be messed up. sort fn should be: sort(a, b) {return a < b} Second when you want to use obj to dedup, the deduped array will be array of strings not array of numbers.

    • @JBuchmann
      @JBuchmann 5 років тому

      That's what I thought initially, but I think it'll work anyway because the sort will group the duplicates together, even if they're not overall in the right order. I believe the algorithm will still remove the duplicates.

  • @nlburnr1
    @nlburnr1 6 років тому +4

    Man I would like to thank you for this channel. I'm really enjoy javascript but I don't feel confident enough to go to a job interview ... I know fpr sure that this and you other channel will boost my confidence thnx you in advance sir

    • @joshwood9503
      @joshwood9503 5 років тому +1

      No one ever feels confident enough for an interview, even after years of experience. Just go do it!

  • @yashwantkaki5072
    @yashwantkaki5072 4 роки тому +2

    Man....that was so clean and neat.... understood all of em

  • @amberlum9166
    @amberlum9166 6 років тому +4

    I was just asked this question in the interview recently!! Thank you so much for this awesome tutorial!!

    • @InterviewNest
      @InterviewNest  6 років тому

      I am glad it helped. Thanks for watching!

  • @thydevdom
    @thydevdom 3 роки тому

    This channel is Lowkey a gold mine. I’m glad I found it. Really simplifies things in such a great way.

    • @InterviewNest
      @InterviewNest  3 роки тому

      Glad you enjoy it! Thanks for watching buddy

  • @javascript_developer
    @javascript_developer 4 роки тому

    Way back I had watch this video to learn to remove duplicate items and now in the interview I got this same question. Initially I show him by using indexOf method and again he was not satisfied with this method saying I will have to push the item to the array everytime so this will consume more time. So I again show him the object property method like you have explain in this video and return the Object.keys(obj) . he was very happy. More couple of question and now I got selected in technical round. Thank you @techshit @interviewnest
    Somehow this channel was not subscribed. I was so unlucky. I subscribed it now.

  • @roslanhristov391
    @roslanhristov391 4 роки тому +23

    Using the Object.keys(obj) way returns string values and you begin with numbers, so shouldn't the interviewer want the values back in the same data type ?

    • @zakachou
      @zakachou 4 роки тому +5

      I had the same remark while watching the video 😁 and I think that it's not going to be faster because you'll need to do another loop to convert them into numbers

    • @javascript_developer
      @javascript_developer 4 роки тому

      @@zakachou its fine until we use === operator. otherwise yes its true that is slow again.

    • @nnavu99
      @nnavu99 4 роки тому +8

      he could've done obj[i] = i, then use Object.values()

    • @elernen9619
      @elernen9619 3 роки тому +2

      let b = Object.keys(obj).map(x => x * 1);

    • @MsRitika1990
      @MsRitika1990 3 роки тому +1

      You can add Number before the element

  • @thamindujanaka6771
    @thamindujanaka6771 4 роки тому

    instead of creating variable for length of the array outside we can do like this, inside for loop
    for (let i=0,j=a.length;i

  • @hamidrezaakbarnezhad2018
    @hamidrezaakbarnezhad2018 3 роки тому +1

    Very Tnks... Good solutions and get best solution step by step👍thats grate

  •  4 роки тому +2

    Actually, using
    let len = a.length;
    for (let i = 0; i < len; i++) { }
    is **slower** than simply writing
    for (let i = 0; i < a.length; i++) { }
    Test it yourself using jsperf. The runtime engine already allocates a variable for length, so doing the same thing manually is only adding to the performance cost.

  • @saskirakosyan5268
    @saskirakosyan5268 5 років тому +6

    your videos are always greate, you are a good theacher. respect!!!

  • @santunu23
    @santunu23 6 років тому +2

    I love your videos, thank you for your time and effort. Removing a duplicate element from an array is a tricky thing.By using collection(in Javascript it is Set) it can be done easily.

  • @imsheldlocked3386
    @imsheldlocked3386 6 років тому +17

    Can you please cover some Javascript design patterns like Module, Factory and Prototype?

  • @tanvirhasanmonir1627
    @tanvirhasanmonir1627 2 роки тому

    Wow! wonderfully explained, Thanks a lot.

  • @joshseipel9891
    @joshseipel9891 2 роки тому

    Great variations with explanations. This is a very common interview question and has always frustrated me! Thanks!

  • @afterthetech4757
    @afterthetech4757 5 років тому +1

    that thing you did at 9:18 is f****** amazing!

  • @schmuf78
    @schmuf78 6 років тому

    Your Style of showing several solutions is super helpful. Thanks for that and keep up.

  • @tusharbhalerao3935
    @tusharbhalerao3935 3 роки тому

    It's an very helpful for me.Thank you so much sir...

  • @purvasawant6169
    @purvasawant6169 5 років тому +2

    Hey, I love your tutorials and the way you explain everything line by line. Liked, Subscribed and already shared your tutorials with my colleagues. Looking forward for your upcoming videos.
    Just noticed,
    Object.keys method provided all the string values in the array.

  • @visithrab2010
    @visithrab2010 3 роки тому

    You are explaining the concept in a very good manner.... thank you so much :)

  • @TheOfficialIH4xx3R
    @TheOfficialIH4xx3R 6 років тому +16

    The Complexity of quicksort is O(n * log n) not O(log n)

    • @johnkeck
      @johnkeck 5 років тому +1

      And that's the BEST-case scenario

    • @k.alipardhan6957
      @k.alipardhan6957 5 років тому

      @@johnkeck big-O is worst case. best and avg to big-omega and big-?

    • @usmaness
      @usmaness 5 років тому

      is the complexity mean how bad/good the code is ? nd these O nd omegas are like the degree of how bad/good is it ?
      ps : I have no idea, just wanted a quickflash clue

    • @minghaoliang4311
      @minghaoliang4311 4 роки тому +1

      @@usmaness It's a way to evaluate how much the time will scale with n for a given algorithm.

  • @lineasdessertdiary
    @lineasdessertdiary 2 роки тому

    Your explanation is amazing.

  • @MrYOUTUB1982
    @MrYOUTUB1982 5 років тому +10

    NIce explanation but the issue with Object approach is that you get strings as an array element back.

    • @sethk7645
      @sethk7645 4 роки тому +1

      Good catch.
      The only way I see of fixing that is to loop through "b[ ]" and parseInt( ) each member of the array. It's a shame that you can't include type conversion as an argument in the Object.keys( ) method.
      In which case, you're better off just looping through the object and pushing each key to a new array while using parseInt, rather than using Object.keys and looping through it again to change the strings to integers.

    • @sethk7645
      @sethk7645 4 роки тому +1

      let b = [ ];
      for( key in obj ){
      b.push( parseInt( key ) );
      }

    • @sethk7645
      @sethk7645 4 роки тому +1

      ​@@ajasen You can still use the object pattern, it's just a bit messier. At the least, you need an extra conditional or two. And the more edge cases, the messier the code gets. This isn't to say that you would actually want to use this approach, but it can be done for what you're asking. I'll put the code for the cases you provided below. I added a couple of extra cases for "false" or "undefined", vs false or undefined (string value vs boolean or undefined data type in these cases).
      P.S. For a Function or Object or BigInt, etc... type as a member of the original array, I would need more logic in my convertToType( ) function, but you get the idea here...

    • @sethk7645
      @sethk7645 4 роки тому

      const a = [1, 2, "52634", 1.2, "false" ,"1", "dog", false, false, 4.5, 5.5, 1.2, 5, "cat", "cat", true, null, undefined, undefined, 2, true, null, "null", false, null, 1, 8];
      function eliminateDups(theArray) {
      let obj = {};
      // I threw in a statement for distinguishing edge cases of strings that could be defined as other types such as "undefined" the string vs undefined the data type or "4" vs 4.
      for (const key of theArray) {
      if(typeof key === "string"){
      obj["%String%"+key] = true;
      } else {
      obj[key] = false;
      }
      }
      let b = [ ];
      //assignment of the temp tests for a string then for a number then converts otherwise;
      for( key in obj ){
      let temp = (obj[key] === true ? key.slice(8) : ( parseFloat(key) || convertToType(key) ));
      b.push( temp );
      }
      console.log("b: ")
      console.log(b);
      return b;
      }
      function convertToType (theKey) {
      switch(theKey){
      case "null":
      return null;
      break;
      case "undefined":
      return undefined;
      break;
      case "false":
      case "true":
      return theKey === "true";
      break;
      default:
      return theKey;
      }
      }
      console.log(eliminateDups(a));

  • @not_enoughmana
    @not_enoughmana 6 років тому

    Dude. Never spread a Set before. Friggin' awesome!!

  • @chamanarts1746
    @chamanarts1746 Рік тому

    Nice video thx for making this video plz make more videos like this. Very informative and easy to understand for the biggners ❤👌plz make more videos on interview questions so that people who do not gave interviews yet and this is the first time going for the interview these questions are very helpful for them ❤❤

  • @nameymcnameson1903
    @nameymcnameson1903 9 місяців тому

    neat i figured you had to convert to a set, then do the spread but i like that i kind of came up with something similar to the one line

  • @kamalkant1753
    @kamalkant1753 6 років тому

    Its real helpful for the script developer thanks for such a nice explanation

  • @moy2010
    @moy2010 6 років тому +1

    Your videos are awesome, techsith. I always use them as reference when I encounter a new problem in code.

  • @dancewithzinnia
    @dancewithzinnia 4 роки тому +3

    You explain concepts so well always! and thank you for mentioning the complexity difference. Very informative! and I saw your closures video but it was somewhat confusing. It would be great if you can explain in more depth with different example

  • @adamgordon862
    @adamgordon862 5 років тому +2

    Awesome video as always, thanks for the multiple explanations!

    • @InterviewNest
      @InterviewNest  5 років тому +1

      Thanks for watching Adam. Keep on learning!

  • @alinamarasca2
    @alinamarasca2 3 роки тому

    Thank you very much! very good tempo and explanations.

  • @passiveBhai
    @passiveBhai 4 роки тому

    What a tricky question and answer. I become a fan of yours.

  • @cancanjaker1620
    @cancanjaker1620 4 роки тому

    It shd be noted that the keys of an object are going to be strings. So by using JS objects, there is going to be a type change unintentionally

  • @Rafa-my3vr
    @Rafa-my3vr 4 роки тому

    you could simplify your loop and just do `for (let i=0, len=a.length; i < len; i++)...

  • @Oswee
    @Oswee 5 років тому +1

    Really neat thing!!! Please do more such a practical puzzle pieces.

  • @amandathompson9697
    @amandathompson9697 4 роки тому

    Your videos explain things SO WELL. Thank you so much!

  • @simonbachmann2120
    @simonbachmann2120 4 роки тому +19

    7:42 quicksort has n*log(n)

  • @atchetna92
    @atchetna92 6 років тому +3

    Thanks for the wonderful concepts. Everytime I watch your video, I always learn something new.
    Do you have an elaborate video on sets and it's work around?

    • @InterviewNest
      @InterviewNest  6 років тому

      On monday i will release a video n maps and weakmap data structures.
      this would be on my techsithtube channel . I will cover sets and weaksets in next. video.

  • @abinetsolomon6076
    @abinetsolomon6076 4 роки тому

    Keep posting more tutorials.the way u explain things r easy to understand.so Am expecting moreeee videos

    • @InterviewNest
      @InterviewNest  4 роки тому

      Abhinet, I also have another channel where I post more videos on JS , React, HTML , CSS ect. its called techsithtube

  • @emreavci3740
    @emreavci3740 2 роки тому

    nice video. instructive and simple.

  • @ghulam-mahyuddinansari2786
    @ghulam-mahyuddinansari2786 Рік тому +1

    Hey a small correction, the complexity for quick sort is nlogn and regarding the a length it doesn't calculates the length every time but is stored and whenever you update values js does it automatically.

  • @lucastperez
    @lucastperez 3 роки тому

    Another one liner could be
    let arr = [ 1, 2, 5, 2, 1, 8 ];
    arr.filter( (num, index) => !ar.slice(0, index).includes(num) );
    Maybe not performatic chaining filter, slice and includes, but at least it is pretty (:
    Also it is worth noticing that the third solution (the one that converts array to object and then get the keys) will make the numbers get converted to string. in the final array In other words, we are getting [ "1", "2", "5", "8" ] as the result.
    We could use the numbers as values instead of "true" and use Object.values(obj) to get them back in array form:
    let arr = [ 1, 2, 5, 2, 1, 8 ];
    let obj = {}
    arr.forEach( num => obj[num] = num )
    Object.values(obj) // this returns [ 1, 2, 5, 8 ]

  • @AAmorim1989
    @AAmorim1989 4 роки тому

    Indeed gotta pay attention: remove duplicates from array !== create an array of unique values.

  • @mukut5ul
    @mukut5ul 4 роки тому

    Good explanation with different approach.

  • @benatakaan613
    @benatakaan613 2 роки тому

    Amazing content! 👏🏻👏🏻👏🏻

  • @isaacb5814
    @isaacb5814 3 роки тому +1

    Subscribed straight away!! This is great stuff! Thank you so much

  • @hari-hi4wf
    @hari-hi4wf 4 роки тому +1

    well explained

  • @onlineeslcanada
    @onlineeslcanada 2 роки тому

    very good content! learning a lot.

  • @mdshoaibAlamgcetts
    @mdshoaibAlamgcetts 6 років тому +1

    Sir this is awesome . thank you so much sir for covering this important question.

    • @InterviewNest
      @InterviewNest  6 років тому +1

      md, keep up the good work and keep practicing!

  • @bhrugudesai
    @bhrugudesai 5 років тому +2

    Great content as always. Just one thing, Can you specify going forward which complexity are you talking about (time complexity or space complexity) because, it sounded like space complexity when you said 2 arrays, and n square, but we only had 1 loop so the time complexity should be only O(n) and not O(n^2) for brute force

    • @ts8960
      @ts8960 2 роки тому

      it also had .includes which loops over the array

  • @akalrove4834
    @akalrove4834 3 роки тому

    Only the last method (Set) works for falsy values like null and undefined. So that should be the correct and most complete answer here.

  • @nickvledder
    @nickvledder 5 років тому

    Very constructive and good to follow tutorial! (From great teacher.)

  • @vin_v
    @vin_v 3 роки тому

    Thanks for the answer sir... marking this question as i have seen it ...also really great how you explained the different ways i which we can achieve the answer...and what's best... my immediate approach though was similar to the brute force method...sat down with the problem and thought of other JS specific way i could solve it... been preparing myself for upcoming interviews and your videos are a great help for it ..thank you

  • @saikrishna8899
    @saikrishna8899 4 роки тому

    3rd solution converts numbers to string that might trigger more questions in the interview

  • @Hunter_Bidens_Crackpipe_
    @Hunter_Bidens_Crackpipe_ Рік тому +1

    Using .length is faster good sir

  • @chriscarey4618
    @chriscarey4618 Рік тому

    A concise explaination, really appreciate your content - subscribed!

  • @hekskey
    @hekskey 3 роки тому

    An alternate way to use an object is like this:
    const a = [1, 2, 5, 2, 1, 8];
    const b = [];
    const arrValMap = {};
    for (const i of a) {
    if (!arrValMap[i]) {
    b.push(i);
    arrValMap[i] = true;
    }
    }
    With this approach, the array values remain numbers rather than being converted to strings.... And I just noticed this video is several years old. Oh well.

  • @FargoLargo
    @FargoLargo 4 роки тому +1

    You are genius man! Ofc i subscribe on you. Keep going shoot you tutorial. Thank you for you programmers community service!

  • @santoshpasupunuri
    @santoshpasupunuri 6 років тому +3

    Awesome! can you do a video on best practices? In this video you mentioned that create a separate variable for a.lenght instead of using in the for loop. Similar to that, can you make a separate video on best practices in javascript or any other programming languages?

  • @chrismaxwell8347
    @chrismaxwell8347 2 роки тому +1

    Using the JavaScript Objects method doesn't return an array of NUMBERS. It's actually an array of strings. example: ["1", "3", "5", "8"]

  • @chharsha231
    @chharsha231 5 років тому

    Bro you are really making sense.. love all your videos.. Big Thanks...

  • @initiala2375
    @initiala2375 4 роки тому

    Very good explanation

  • @SatishRMopur
    @SatishRMopur 5 років тому

    Thank you so much for explaining it so well with all possible solutions and the best one.

  • @LJ-lw8qd
    @LJ-lw8qd 2 роки тому

    Great video, liked and subscribed....very clear, precise and the explanations are great. Thanks. Going to look at more of your work now 👍

  • @gosinP
    @gosinP 3 роки тому

    Please, make more videos like this one. Thank you.

    • @InterviewNest
      @InterviewNest  3 роки тому +1

      More to come!

    • @gosinP
      @gosinP 3 роки тому

      @@InterviewNest if you are interested to do JS interview with me for your channel, my email is milanpjevic@gmail.com

  •  5 років тому

    In the first algorithm, you have the variable len defined globally, which is unnecessary (the same reason why we put let i = 0). It's better to write the for loop as this:
    for(let i = 0, len = a.length; i < len; i++)
    This way, a.length is accessed only once, which is good, but len is cleared from memory the moment the loop finishes.

  • @balognerjohnerjohnson6258
    @balognerjohnerjohnson6258 4 роки тому

    damn wish i found you sooner and that i should've studied more. Gonna watch more of your videos and hopefully i'll get better at coding. Thank you so much for these.

  • @mrmatt8998
    @mrmatt8998 6 років тому

    i like your approach with different methods :)

  • @designxperts9815
    @designxperts9815 3 роки тому

    instead of above concept we can use console.log([...new Set(nums)]) will return the unique value. for ascending and descending we can use reverse and sort can use.. your comment required here Sir

    • @InterviewNest
      @InterviewNest  3 роки тому +1

      Santosh, yes that is also a simplest way to remove duplicate values from an array.

  • @rahulsarath8680
    @rahulsarath8680 5 років тому +1

    sir please make more interview programming question ,this helps me confident in the interview

    • @bijaykurra9989
      @bijaykurra9989 5 років тому

      please share your interview question.i doing struggle for getting job

  • @SmartProgramming
    @SmartProgramming 5 років тому

    nicely explained, keep it up sir, thank you 👍👍👍👍🙂🙂🙂🙂

  • @dragonore2009
    @dragonore2009 4 роки тому

    Of course there is filter you can use as well.
    let b = a.filter( val, idx, arr) => arr.indexOf(val) === idx;

  • @ankithprabhu09
    @ankithprabhu09 6 років тому

    Using the Set and spread operator is a cool solution

  • @prathprath265
    @prathprath265 5 років тому +6

    var array = [1, 1, 2, 3, 4, 4, 5];
    var filtered = [];
    for (var i = 0; i < array.length; i++) {
    if (!filtered.includes(array[i])) {
    filtered.push(array[i])
    }
    }
    console.log(filtered)

    • @danialmalik80
      @danialmalik80 5 років тому

      still N^2 complexity

    • @CknSalad
      @CknSalad 5 років тому +3

      @@danialmalik80 yup, because 'includes' or 'indexOf' is essentially a for-loop that checks for the existence of an item (number in this case). Therefore, there is an outer loop (for-loop) and an inner loop ('includes) -> N^2

  • @ToddsDiscGolf
    @ToddsDiscGolf 2 роки тому

    Awesome tutorial thank you so much!

  • @AdarshSingh-qd6mq
    @AdarshSingh-qd6mq 3 роки тому

    Awesome one

  • @Cloud9er
    @Cloud9er 3 роки тому

    I need more of these videos

  • @teresitaambrosio4503
    @teresitaambrosio4503 6 років тому

    Thank you so much for your explanation, it was SO clear!! It's difficult to find this type of explanations so THANK YOU :)

  • @micowata
    @micowata 4 роки тому

    excellent pedagogy, thank you sir ! :)

  • @manishmaithani398
    @manishmaithani398 6 років тому +2

    Great work man. One request, can you please add videos on datatypes fromJS, Map, List, Set (import { fromJS, Map, List, Set } from 'immutable' )

    • @InterviewNest
      @InterviewNest  6 років тому +2

      On monday i will release a video n maps and weakmap data structures. this would be on my techsithtube channel . I will cover sets next.

    • @thegreatcheater7154
      @thegreatcheater7154 6 років тому

      InterviewNest thx

  • @doug_horne
    @doug_horne 4 роки тому +1

    Great tutorial. Thanks!

  • @robertsullivan6246
    @robertsullivan6246 Рік тому

    Great for arrays of primitive types. What about arrays of objects when you want to exclude objects that are equal by content?

  • @davidlee588
    @davidlee588 2 роки тому

    The LeetCode specified that "Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory."

  • @thehonestabe
    @thehonestabe 4 роки тому

    Nice tutorial! It has a lot of information!!!

  • @godsonraizen
    @godsonraizen 3 роки тому +1

    Wouldn't the complexity of the brute force method be just O(N) seeing as you're looping through the array just once and there are no nested loops?

    • @InterviewNest
      @InterviewNest  3 роки тому

      Depending on which approach you take. If you sort it before looping through than it s O(nlogn) because best sorting algo takes that complexity

  • @poojajethva
    @poojajethva 4 роки тому

    Using Object.keys(obj) the array values we are getting are in string

  • @samitdigitalart
    @samitdigitalart 4 роки тому

    Thanks. Learnt new things and got new ideas.

  • @LifeAtMH41
    @LifeAtMH41 3 роки тому

    Using Object.keys() will produce your resultant array as string values. Those string values must be converted to numbers.

  • @smyk1975
    @smyk1975 3 роки тому

    The complexity of a quick sort is n*log(n). Also I’m not sure if JS objects can have say, millions of elements?

  • @AyanBhadury
    @AyanBhadury 4 роки тому

    when we are using Object.keys approach, it returns the string values as the elements, hence we again need to convert this to an integer using the array.map, which in turns increases the complexity
    let a = Object.keys(obj).map((value)=>{
    return parseInt(value, 10);
    })

  • @MrB_eastt
    @MrB_eastt 5 років тому

    Sort() function sometimes does work good with number you need to write comparison function like a.sort(function (a,b)=> a-b)
    Thank you

    • @CknSalad
      @CknSalad 5 років тому

      yup, it will not work for something like [1, 11, 2, 3, 33, 4].

    • @sethk7645
      @sethk7645 4 роки тому

      For this purpose, it doesn't matter, so long as all identical elements are next to each other. [1,1,1,11,11,2,23,23,3,3,3,33,4] would work because we're only pushing a new item to the resulting array "b", if the previous item wasn't the same as the current one.

  • @GautamKumar-uideveloper
    @GautamKumar-uideveloper 6 років тому +1

    Awesome video, please provide a video if we do this with filter().

  • @pratikfulkar4335
    @pratikfulkar4335 3 роки тому

    great work

  • @hebrux
    @hebrux 6 років тому

    The first example is my favorite because I had already tried to solve this algorithm using a similar method but failed. the problem with my original attempt was that it only looked at the number previous to current selection. So there were still duplicates in the array.