Remove all of the duplicate numbers in an array of numbers [duplicate]Get all unique values in a JavaScript array (remove duplicates)Get all non-unique values (i.e.: duplicate/more than one occurrence) in an arrayRemove occurrences of duplicate words in a stringremove all elements that occur more than once from arrayCreate ArrayList from arrayHow do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?How to replace all occurrences of a string in JavaScriptLoop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?Remove duplicate values from JS arrayFor-each over an array in JavaScript?

"Oh no!" in Latin

Why is the principal energy of an electron lower for excited electrons in a higher energy state?

Why didn’t Eve recognize the little cockroach as a living organism?

Is there anyway, I can have two passwords for my wi-fi

What happens if I try to grapple mirror image?

El Dorado Word Puzzle II: Videogame Edition

Is stochastic gradient descent pseudo-stochastic?

Personal or impersonal in a technical resume

Cumulative Sum using Java 8 stream API

Confusion over Hunter with Crossbow Expert and Giant Killer

How much do grades matter for a future academia position?

Possible Eco thriller, man invents a device to remove rain from glass

Is there a distance limit for minecart tracks?

Showing mass murder in a kid's book

Difference between shutdown options

Isometric embedding of a genus g surface

How to leave product feedback on macOS?

Do I have to take mana from my deck or hand when tapping a dual land?

Ways of geometrical multiplication

What is the smallest number n> 5 so that 5 ^ n ends with "3125"?

Why can't the Brexit deadlock in the UK parliament be solved with a plurality vote?

How do I prevent inappropriate ads from appearing in my game?

Would a primitive species be able to learn English from reading books alone?

Why do Radio Buttons not fill the entire outer circle?



Remove all of the duplicate numbers in an array of numbers [duplicate]


Get all unique values in a JavaScript array (remove duplicates)Get all non-unique values (i.e.: duplicate/more than one occurrence) in an arrayRemove occurrences of duplicate words in a stringremove all elements that occur more than once from arrayCreate ArrayList from arrayHow do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?How to replace all occurrences of a string in JavaScriptLoop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?Remove duplicate values from JS arrayFor-each over an array in JavaScript?













10
















This question already has an answer here:



  • Get all unique values in a JavaScript array (remove duplicates)

    66 answers



I received this question for practice and the wording confused me, as I see 2 results that it might want.



And either way, I'd like to see both solutions.



For example, if I have an array:



let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];


I'm taking this as wanting the final result as either:



let finalResult = [1, 2, 3, 4, 5, 8, 9, 10];


OR:



let finalResult = [1, 9, 10];


The difference between the two being, one just removes any duplicate numbers and leaves the rest and the second just wants any number that isn't a duplicate.



Either way, I'd like to write two functions that does one of each.



This, given by someone else gives my second solution.



let elems = ,

arr2 = arr.filter(function (e)
if (elems[e] === undefined)
elems[e] = true;
return true;

return false;
);
console.log(arr2);


I'm not sure about a function for the first one (remove all duplicates).










share|improve this question















marked as duplicate by Jared Smith, pushkin, BlueRaja - Danny Pflughoeft, the_lotus, Moira 3 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • If you're using lodash, you can use _.uniq()

    – BlueRaja - Danny Pflughoeft
    12 hours ago






  • 1





    Further, this is asking for the inverse of Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array. Finally, this post is asking two separate questions and both have good answers elsewhere already.

    – Søren D. Ptæus
    12 hours ago












  • To answer the question "which one is it" in a comment-answer: if you're asked to remove duplicates, I believe you should understand the first variant. The second variant removes all element that have duplicates, meaning the "original" value AND its duplicates.

    – Pierre Arlaud
    9 hours ago















10
















This question already has an answer here:



  • Get all unique values in a JavaScript array (remove duplicates)

    66 answers



I received this question for practice and the wording confused me, as I see 2 results that it might want.



And either way, I'd like to see both solutions.



For example, if I have an array:



let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];


I'm taking this as wanting the final result as either:



let finalResult = [1, 2, 3, 4, 5, 8, 9, 10];


OR:



let finalResult = [1, 9, 10];


The difference between the two being, one just removes any duplicate numbers and leaves the rest and the second just wants any number that isn't a duplicate.



Either way, I'd like to write two functions that does one of each.



This, given by someone else gives my second solution.



let elems = ,

arr2 = arr.filter(function (e)
if (elems[e] === undefined)
elems[e] = true;
return true;

return false;
);
console.log(arr2);


I'm not sure about a function for the first one (remove all duplicates).










share|improve this question















marked as duplicate by Jared Smith, pushkin, BlueRaja - Danny Pflughoeft, the_lotus, Moira 3 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • If you're using lodash, you can use _.uniq()

    – BlueRaja - Danny Pflughoeft
    12 hours ago






  • 1





    Further, this is asking for the inverse of Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array. Finally, this post is asking two separate questions and both have good answers elsewhere already.

    – Søren D. Ptæus
    12 hours ago












  • To answer the question "which one is it" in a comment-answer: if you're asked to remove duplicates, I believe you should understand the first variant. The second variant removes all element that have duplicates, meaning the "original" value AND its duplicates.

    – Pierre Arlaud
    9 hours ago













10












10








10


2







This question already has an answer here:



  • Get all unique values in a JavaScript array (remove duplicates)

    66 answers



I received this question for practice and the wording confused me, as I see 2 results that it might want.



And either way, I'd like to see both solutions.



For example, if I have an array:



let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];


I'm taking this as wanting the final result as either:



let finalResult = [1, 2, 3, 4, 5, 8, 9, 10];


OR:



let finalResult = [1, 9, 10];


The difference between the two being, one just removes any duplicate numbers and leaves the rest and the second just wants any number that isn't a duplicate.



Either way, I'd like to write two functions that does one of each.



This, given by someone else gives my second solution.



let elems = ,

arr2 = arr.filter(function (e)
if (elems[e] === undefined)
elems[e] = true;
return true;

return false;
);
console.log(arr2);


I'm not sure about a function for the first one (remove all duplicates).










share|improve this question

















This question already has an answer here:



  • Get all unique values in a JavaScript array (remove duplicates)

    66 answers



I received this question for practice and the wording confused me, as I see 2 results that it might want.



And either way, I'd like to see both solutions.



For example, if I have an array:



let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];


I'm taking this as wanting the final result as either:



let finalResult = [1, 2, 3, 4, 5, 8, 9, 10];


OR:



let finalResult = [1, 9, 10];


The difference between the two being, one just removes any duplicate numbers and leaves the rest and the second just wants any number that isn't a duplicate.



Either way, I'd like to write two functions that does one of each.



This, given by someone else gives my second solution.



let elems = ,

arr2 = arr.filter(function (e)
if (elems[e] === undefined)
elems[e] = true;
return true;

return false;
);
console.log(arr2);


I'm not sure about a function for the first one (remove all duplicates).





This question already has an answer here:



  • Get all unique values in a JavaScript array (remove duplicates)

    66 answers







javascript arrays duplicates






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 4 hours ago









pushkin

4,298112954




4,298112954










asked 15 hours ago









mph85mph85

1089




1089




marked as duplicate by Jared Smith, pushkin, BlueRaja - Danny Pflughoeft, the_lotus, Moira 3 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Jared Smith, pushkin, BlueRaja - Danny Pflughoeft, the_lotus, Moira 3 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • If you're using lodash, you can use _.uniq()

    – BlueRaja - Danny Pflughoeft
    12 hours ago






  • 1





    Further, this is asking for the inverse of Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array. Finally, this post is asking two separate questions and both have good answers elsewhere already.

    – Søren D. Ptæus
    12 hours ago












  • To answer the question "which one is it" in a comment-answer: if you're asked to remove duplicates, I believe you should understand the first variant. The second variant removes all element that have duplicates, meaning the "original" value AND its duplicates.

    – Pierre Arlaud
    9 hours ago

















  • If you're using lodash, you can use _.uniq()

    – BlueRaja - Danny Pflughoeft
    12 hours ago






  • 1





    Further, this is asking for the inverse of Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array. Finally, this post is asking two separate questions and both have good answers elsewhere already.

    – Søren D. Ptæus
    12 hours ago












  • To answer the question "which one is it" in a comment-answer: if you're asked to remove duplicates, I believe you should understand the first variant. The second variant removes all element that have duplicates, meaning the "original" value AND its duplicates.

    – Pierre Arlaud
    9 hours ago
















If you're using lodash, you can use _.uniq()

– BlueRaja - Danny Pflughoeft
12 hours ago





If you're using lodash, you can use _.uniq()

– BlueRaja - Danny Pflughoeft
12 hours ago




1




1





Further, this is asking for the inverse of Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array. Finally, this post is asking two separate questions and both have good answers elsewhere already.

– Søren D. Ptæus
12 hours ago






Further, this is asking for the inverse of Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array. Finally, this post is asking two separate questions and both have good answers elsewhere already.

– Søren D. Ptæus
12 hours ago














To answer the question "which one is it" in a comment-answer: if you're asked to remove duplicates, I believe you should understand the first variant. The second variant removes all element that have duplicates, meaning the "original" value AND its duplicates.

– Pierre Arlaud
9 hours ago





To answer the question "which one is it" in a comment-answer: if you're asked to remove duplicates, I believe you should understand the first variant. The second variant removes all element that have duplicates, meaning the "original" value AND its duplicates.

– Pierre Arlaud
9 hours ago












9 Answers
9






active

oldest

votes


















16














Using Set and Array.from()






let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

console.log(Array.from(new Set(arr)));





Alternate using regex



regex explanation here






let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

let res = arr
.join(',')
.replace(/(b,w+b)(?=.*1)/ig, '')
.split(',')
.map(Number);

console.log(res);





Alternate using objects






let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

let obj = arr.reduce((acc, val) => Object.assign(acc,
[val]: val
), );

console.log(Object.values(obj));








share|improve this answer
































    13














    Just use a simple array.filter one-liner:






    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
    let finalResult = arr.filter((e, i, a) => a.indexOf(e) == i).sort(function(a, b)return a - b);
    console.log(finalResult);





    You could use another filter statement if you wanted the second result:






    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
    let finalResult = arr.filter((e, i, a) => a.filter(f => f == e).length == 1).sort(function(a, b)return a - b);
    console.log(finalResult);








    share|improve this answer

























    • You could also add .sort() to sort them by numerical order: .sort(function(a, b)return a - b) on finalresult

      – Mukyuu
      15 hours ago











    • Yes @Mukyuu, that would also be useful

      – Jack Bashford
      15 hours ago






    • 2





      Worth pointing out that the run time of this approach will be quadratic on the size of the input, which is probably not great unless the input arrays are known to be always fairly small.

      – Joe Lee-Moyet
      10 hours ago











    • Most voted with multiple array#filter, array#sort and array#indexOf... That is not performant

      – Yosvel Quintero
      9 hours ago












    • Do note that the .sort() is not necessary - the end result without the sort is still an array without any duplicate items, just in the same order as the original array. (It does make it exactly match the finalResult variable in the question though.)

      – Florrie
      7 hours ago


















    9














    For the first part you can use Set() and Spread Syntax to remove duplicates.






    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
    let res = [...new Set(arr)]
    console.log(res)





    For the second part you can use reduce()






    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
    //to get the object with count of each number in array.
    let obj = arr.reduce((ac,a) =>
    //check if number doesnot occur before then set its count to 1
    if(!ac[a]) ac[a] = 1;
    //if number is already in object increase its count
    else ac[a]++;
    return ac;
    ,)
    //Using reduce on all the keys of object means all numbers.
    let res = Object.keys(obj).reduce((ac,a) =>
    //check if count of current number 'a' is `1` in the above object then add it into array
    if(obj[a] === 1) ac.push(+a)
    return ac;
    ,[])
    console.log(res)








    share|improve this answer

























    • nice appreciate that, that 2nd one looks crazy. I'm assuming the time complexity for would be less than ideal compared to other results?

      – mph85
      15 hours ago











    • @mph85 Yes its a little complex because it doesnot go through the array again and again instead it just store all the result obj and then filter it.Its better regarding performance

      – Maheer Ali
      14 hours ago












    • could you remind me why we need the spread operator? what happens if we don't have it? @Maheer Ali

      – mph85
      14 hours ago












    • Is it because if we don't have it, it'll just log an object?

      – mph85
      14 hours ago











    • @mph85 No if we will not have it. We will have a Set() inside array. We use it convert Set() to Array

      – Maheer Ali
      14 hours ago


















    4














    You could sort the array before and filter the array by checking only one side for duplicates or both sides.






    var array = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10],
    result1,
    result2;

    array.sort((a, b) => a - b);

    result1 = array.filter((v, i, a) => a[i - 1] !== v);
    result2 = array.filter((v, i, a) => a[i - 1] !== v && a[i + 1] !== v);

    console.log(...result1);
    console.log(...result2)








    share|improve this answer























    • thank for that, for the result1 what is going on with the a[i - 1] !== v?

      – mph85
      23 mins ago











    • a is the array, i is the actual index, and v is the actual value. it takes the value from the index before of the actual index and looks if the values are not equal.

      – Nina Scholz
      21 mins ago











    • ah ok, and sorry, it takes the value from the index before of the actual index? Not sure what you mean by that

      – mph85
      14 mins ago












    • for example if you have the value 9 of the array as v, then the value before is 8.

      – Nina Scholz
      11 mins ago











    • ah ok, thank you for that

      – mph85
      9 mins ago



















    4














    You can create both arrays in One Go






    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
    let unique = new Set();
    let repeated = Array.from(arr.reduce((acc, curr) =>
    acc.has(curr) ? unique.delete(curr) : acc.add(curr) && unique.add(curr);
    return acc;
    , new Set()));

    console.log(Array.from(unique))
    console.log(repeated)








    share|improve this answer


















    • 1





      Nice one mate +1

      – Maheer Ali
      14 hours ago











    • I wonder if the ternary plus && could be unclear though (x?y:z&&w)? It's not obvious to me how JS's order of operations would handle that, and I wonder if you could get across the same logic and reasoning by using if/else?

      – Florrie
      7 hours ago


















    4














    You can do:






    const arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
    const hash = arr.reduce((a, c) => (a[c] = (a[c] || 0) + 1, a), );

    // [1, 2, 3, 4, 5, 8, 9, 10];
    const finalResultOne = Object.keys(hash);

    // [1, 9, 10];
    const finalResultTwo = Object.keys(hash).filter(k => hash[k] === 1);

    console.log('finalResultOne:', ...finalResultOne);
    console.log('finalResultTwo:', ...finalResultTwo);








    share|improve this answer
































      3














      You can use closure and Map






      let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

      const build = ar =>
      const mapObj = ar.reduce((acc, e) =>
      acc.has(e) ? acc.set(e, true) : acc.set(e, false)
      return acc
      , new Map())

      return function(hasDup = true)
      if(hasDup) return [...mapObj.keys()]
      else return [...mapObj].filter(([key, val]) => !val).map(([k, v])=> k)



      const getArr = build(arr)

      console.log(getArr())
      console.log(getArr(false))








      share|improve this answer






























        3














        As many other have said, the first one is just [...new Set(arr)]



        For the second, just filter out those that occur more than once:




        const arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

        const count = (arr, e) => arr.filter(n => n == e).length

        const unique = arr => arr.filter(e => count(arr, e) < 2)

        console.log(unique(arr));








        share|improve this answer






























          0














          var arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
          var map = ;
          var finalResult = [];
          for(var i = 0; i < arr.length; i++)
          if(!map.hasOwnProperty(arr[i]))
          map[arr[i]] = true;
          finalResult.push(arr[i]);



          //if you need it sorted otherwise it will be in order
          finalResult.sort(function(a, b)return a-b);





          share|improve this answer





























            9 Answers
            9






            active

            oldest

            votes








            9 Answers
            9






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            16














            Using Set and Array.from()






            let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

            console.log(Array.from(new Set(arr)));





            Alternate using regex



            regex explanation here






            let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

            let res = arr
            .join(',')
            .replace(/(b,w+b)(?=.*1)/ig, '')
            .split(',')
            .map(Number);

            console.log(res);





            Alternate using objects






            let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

            let obj = arr.reduce((acc, val) => Object.assign(acc,
            [val]: val
            ), );

            console.log(Object.values(obj));








            share|improve this answer





























              16














              Using Set and Array.from()






              let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

              console.log(Array.from(new Set(arr)));





              Alternate using regex



              regex explanation here






              let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

              let res = arr
              .join(',')
              .replace(/(b,w+b)(?=.*1)/ig, '')
              .split(',')
              .map(Number);

              console.log(res);





              Alternate using objects






              let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

              let obj = arr.reduce((acc, val) => Object.assign(acc,
              [val]: val
              ), );

              console.log(Object.values(obj));








              share|improve this answer



























                16












                16








                16







                Using Set and Array.from()






                let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                console.log(Array.from(new Set(arr)));





                Alternate using regex



                regex explanation here






                let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                let res = arr
                .join(',')
                .replace(/(b,w+b)(?=.*1)/ig, '')
                .split(',')
                .map(Number);

                console.log(res);





                Alternate using objects






                let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                let obj = arr.reduce((acc, val) => Object.assign(acc,
                [val]: val
                ), );

                console.log(Object.values(obj));








                share|improve this answer















                Using Set and Array.from()






                let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                console.log(Array.from(new Set(arr)));





                Alternate using regex



                regex explanation here






                let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                let res = arr
                .join(',')
                .replace(/(b,w+b)(?=.*1)/ig, '')
                .split(',')
                .map(Number);

                console.log(res);





                Alternate using objects






                let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                let obj = arr.reduce((acc, val) => Object.assign(acc,
                [val]: val
                ), );

                console.log(Object.values(obj));








                let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                console.log(Array.from(new Set(arr)));





                let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                console.log(Array.from(new Set(arr)));





                let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                let res = arr
                .join(',')
                .replace(/(b,w+b)(?=.*1)/ig, '')
                .split(',')
                .map(Number);

                console.log(res);





                let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                let res = arr
                .join(',')
                .replace(/(b,w+b)(?=.*1)/ig, '')
                .split(',')
                .map(Number);

                console.log(res);





                let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                let obj = arr.reduce((acc, val) => Object.assign(acc,
                [val]: val
                ), );

                console.log(Object.values(obj));





                let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                let obj = arr.reduce((acc, val) => Object.assign(acc,
                [val]: val
                ), );

                console.log(Object.values(obj));






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 14 hours ago

























                answered 15 hours ago









                Aswin KumarAswin Kumar

                784115




                784115























                    13














                    Just use a simple array.filter one-liner:






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let finalResult = arr.filter((e, i, a) => a.indexOf(e) == i).sort(function(a, b)return a - b);
                    console.log(finalResult);





                    You could use another filter statement if you wanted the second result:






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let finalResult = arr.filter((e, i, a) => a.filter(f => f == e).length == 1).sort(function(a, b)return a - b);
                    console.log(finalResult);








                    share|improve this answer

























                    • You could also add .sort() to sort them by numerical order: .sort(function(a, b)return a - b) on finalresult

                      – Mukyuu
                      15 hours ago











                    • Yes @Mukyuu, that would also be useful

                      – Jack Bashford
                      15 hours ago






                    • 2





                      Worth pointing out that the run time of this approach will be quadratic on the size of the input, which is probably not great unless the input arrays are known to be always fairly small.

                      – Joe Lee-Moyet
                      10 hours ago











                    • Most voted with multiple array#filter, array#sort and array#indexOf... That is not performant

                      – Yosvel Quintero
                      9 hours ago












                    • Do note that the .sort() is not necessary - the end result without the sort is still an array without any duplicate items, just in the same order as the original array. (It does make it exactly match the finalResult variable in the question though.)

                      – Florrie
                      7 hours ago















                    13














                    Just use a simple array.filter one-liner:






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let finalResult = arr.filter((e, i, a) => a.indexOf(e) == i).sort(function(a, b)return a - b);
                    console.log(finalResult);





                    You could use another filter statement if you wanted the second result:






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let finalResult = arr.filter((e, i, a) => a.filter(f => f == e).length == 1).sort(function(a, b)return a - b);
                    console.log(finalResult);








                    share|improve this answer

























                    • You could also add .sort() to sort them by numerical order: .sort(function(a, b)return a - b) on finalresult

                      – Mukyuu
                      15 hours ago











                    • Yes @Mukyuu, that would also be useful

                      – Jack Bashford
                      15 hours ago






                    • 2





                      Worth pointing out that the run time of this approach will be quadratic on the size of the input, which is probably not great unless the input arrays are known to be always fairly small.

                      – Joe Lee-Moyet
                      10 hours ago











                    • Most voted with multiple array#filter, array#sort and array#indexOf... That is not performant

                      – Yosvel Quintero
                      9 hours ago












                    • Do note that the .sort() is not necessary - the end result without the sort is still an array without any duplicate items, just in the same order as the original array. (It does make it exactly match the finalResult variable in the question though.)

                      – Florrie
                      7 hours ago













                    13












                    13








                    13







                    Just use a simple array.filter one-liner:






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let finalResult = arr.filter((e, i, a) => a.indexOf(e) == i).sort(function(a, b)return a - b);
                    console.log(finalResult);





                    You could use another filter statement if you wanted the second result:






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let finalResult = arr.filter((e, i, a) => a.filter(f => f == e).length == 1).sort(function(a, b)return a - b);
                    console.log(finalResult);








                    share|improve this answer















                    Just use a simple array.filter one-liner:






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let finalResult = arr.filter((e, i, a) => a.indexOf(e) == i).sort(function(a, b)return a - b);
                    console.log(finalResult);





                    You could use another filter statement if you wanted the second result:






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let finalResult = arr.filter((e, i, a) => a.filter(f => f == e).length == 1).sort(function(a, b)return a - b);
                    console.log(finalResult);








                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let finalResult = arr.filter((e, i, a) => a.indexOf(e) == i).sort(function(a, b)return a - b);
                    console.log(finalResult);





                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let finalResult = arr.filter((e, i, a) => a.indexOf(e) == i).sort(function(a, b)return a - b);
                    console.log(finalResult);





                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let finalResult = arr.filter((e, i, a) => a.filter(f => f == e).length == 1).sort(function(a, b)return a - b);
                    console.log(finalResult);





                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let finalResult = arr.filter((e, i, a) => a.filter(f => f == e).length == 1).sort(function(a, b)return a - b);
                    console.log(finalResult);






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 15 hours ago









                    Mukyuu

                    1,78031123




                    1,78031123










                    answered 15 hours ago









                    Jack BashfordJack Bashford

                    12.3k31847




                    12.3k31847












                    • You could also add .sort() to sort them by numerical order: .sort(function(a, b)return a - b) on finalresult

                      – Mukyuu
                      15 hours ago











                    • Yes @Mukyuu, that would also be useful

                      – Jack Bashford
                      15 hours ago






                    • 2





                      Worth pointing out that the run time of this approach will be quadratic on the size of the input, which is probably not great unless the input arrays are known to be always fairly small.

                      – Joe Lee-Moyet
                      10 hours ago











                    • Most voted with multiple array#filter, array#sort and array#indexOf... That is not performant

                      – Yosvel Quintero
                      9 hours ago












                    • Do note that the .sort() is not necessary - the end result without the sort is still an array without any duplicate items, just in the same order as the original array. (It does make it exactly match the finalResult variable in the question though.)

                      – Florrie
                      7 hours ago

















                    • You could also add .sort() to sort them by numerical order: .sort(function(a, b)return a - b) on finalresult

                      – Mukyuu
                      15 hours ago











                    • Yes @Mukyuu, that would also be useful

                      – Jack Bashford
                      15 hours ago






                    • 2





                      Worth pointing out that the run time of this approach will be quadratic on the size of the input, which is probably not great unless the input arrays are known to be always fairly small.

                      – Joe Lee-Moyet
                      10 hours ago











                    • Most voted with multiple array#filter, array#sort and array#indexOf... That is not performant

                      – Yosvel Quintero
                      9 hours ago












                    • Do note that the .sort() is not necessary - the end result without the sort is still an array without any duplicate items, just in the same order as the original array. (It does make it exactly match the finalResult variable in the question though.)

                      – Florrie
                      7 hours ago
















                    You could also add .sort() to sort them by numerical order: .sort(function(a, b)return a - b) on finalresult

                    – Mukyuu
                    15 hours ago





                    You could also add .sort() to sort them by numerical order: .sort(function(a, b)return a - b) on finalresult

                    – Mukyuu
                    15 hours ago













                    Yes @Mukyuu, that would also be useful

                    – Jack Bashford
                    15 hours ago





                    Yes @Mukyuu, that would also be useful

                    – Jack Bashford
                    15 hours ago




                    2




                    2





                    Worth pointing out that the run time of this approach will be quadratic on the size of the input, which is probably not great unless the input arrays are known to be always fairly small.

                    – Joe Lee-Moyet
                    10 hours ago





                    Worth pointing out that the run time of this approach will be quadratic on the size of the input, which is probably not great unless the input arrays are known to be always fairly small.

                    – Joe Lee-Moyet
                    10 hours ago













                    Most voted with multiple array#filter, array#sort and array#indexOf... That is not performant

                    – Yosvel Quintero
                    9 hours ago






                    Most voted with multiple array#filter, array#sort and array#indexOf... That is not performant

                    – Yosvel Quintero
                    9 hours ago














                    Do note that the .sort() is not necessary - the end result without the sort is still an array without any duplicate items, just in the same order as the original array. (It does make it exactly match the finalResult variable in the question though.)

                    – Florrie
                    7 hours ago





                    Do note that the .sort() is not necessary - the end result without the sort is still an array without any duplicate items, just in the same order as the original array. (It does make it exactly match the finalResult variable in the question though.)

                    – Florrie
                    7 hours ago











                    9














                    For the first part you can use Set() and Spread Syntax to remove duplicates.






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let res = [...new Set(arr)]
                    console.log(res)





                    For the second part you can use reduce()






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    //to get the object with count of each number in array.
                    let obj = arr.reduce((ac,a) =>
                    //check if number doesnot occur before then set its count to 1
                    if(!ac[a]) ac[a] = 1;
                    //if number is already in object increase its count
                    else ac[a]++;
                    return ac;
                    ,)
                    //Using reduce on all the keys of object means all numbers.
                    let res = Object.keys(obj).reduce((ac,a) =>
                    //check if count of current number 'a' is `1` in the above object then add it into array
                    if(obj[a] === 1) ac.push(+a)
                    return ac;
                    ,[])
                    console.log(res)








                    share|improve this answer

























                    • nice appreciate that, that 2nd one looks crazy. I'm assuming the time complexity for would be less than ideal compared to other results?

                      – mph85
                      15 hours ago











                    • @mph85 Yes its a little complex because it doesnot go through the array again and again instead it just store all the result obj and then filter it.Its better regarding performance

                      – Maheer Ali
                      14 hours ago












                    • could you remind me why we need the spread operator? what happens if we don't have it? @Maheer Ali

                      – mph85
                      14 hours ago












                    • Is it because if we don't have it, it'll just log an object?

                      – mph85
                      14 hours ago











                    • @mph85 No if we will not have it. We will have a Set() inside array. We use it convert Set() to Array

                      – Maheer Ali
                      14 hours ago















                    9














                    For the first part you can use Set() and Spread Syntax to remove duplicates.






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let res = [...new Set(arr)]
                    console.log(res)





                    For the second part you can use reduce()






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    //to get the object with count of each number in array.
                    let obj = arr.reduce((ac,a) =>
                    //check if number doesnot occur before then set its count to 1
                    if(!ac[a]) ac[a] = 1;
                    //if number is already in object increase its count
                    else ac[a]++;
                    return ac;
                    ,)
                    //Using reduce on all the keys of object means all numbers.
                    let res = Object.keys(obj).reduce((ac,a) =>
                    //check if count of current number 'a' is `1` in the above object then add it into array
                    if(obj[a] === 1) ac.push(+a)
                    return ac;
                    ,[])
                    console.log(res)








                    share|improve this answer

























                    • nice appreciate that, that 2nd one looks crazy. I'm assuming the time complexity for would be less than ideal compared to other results?

                      – mph85
                      15 hours ago











                    • @mph85 Yes its a little complex because it doesnot go through the array again and again instead it just store all the result obj and then filter it.Its better regarding performance

                      – Maheer Ali
                      14 hours ago












                    • could you remind me why we need the spread operator? what happens if we don't have it? @Maheer Ali

                      – mph85
                      14 hours ago












                    • Is it because if we don't have it, it'll just log an object?

                      – mph85
                      14 hours ago











                    • @mph85 No if we will not have it. We will have a Set() inside array. We use it convert Set() to Array

                      – Maheer Ali
                      14 hours ago













                    9












                    9








                    9







                    For the first part you can use Set() and Spread Syntax to remove duplicates.






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let res = [...new Set(arr)]
                    console.log(res)





                    For the second part you can use reduce()






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    //to get the object with count of each number in array.
                    let obj = arr.reduce((ac,a) =>
                    //check if number doesnot occur before then set its count to 1
                    if(!ac[a]) ac[a] = 1;
                    //if number is already in object increase its count
                    else ac[a]++;
                    return ac;
                    ,)
                    //Using reduce on all the keys of object means all numbers.
                    let res = Object.keys(obj).reduce((ac,a) =>
                    //check if count of current number 'a' is `1` in the above object then add it into array
                    if(obj[a] === 1) ac.push(+a)
                    return ac;
                    ,[])
                    console.log(res)








                    share|improve this answer















                    For the first part you can use Set() and Spread Syntax to remove duplicates.






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let res = [...new Set(arr)]
                    console.log(res)





                    For the second part you can use reduce()






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    //to get the object with count of each number in array.
                    let obj = arr.reduce((ac,a) =>
                    //check if number doesnot occur before then set its count to 1
                    if(!ac[a]) ac[a] = 1;
                    //if number is already in object increase its count
                    else ac[a]++;
                    return ac;
                    ,)
                    //Using reduce on all the keys of object means all numbers.
                    let res = Object.keys(obj).reduce((ac,a) =>
                    //check if count of current number 'a' is `1` in the above object then add it into array
                    if(obj[a] === 1) ac.push(+a)
                    return ac;
                    ,[])
                    console.log(res)








                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let res = [...new Set(arr)]
                    console.log(res)





                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let res = [...new Set(arr)]
                    console.log(res)





                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    //to get the object with count of each number in array.
                    let obj = arr.reduce((ac,a) =>
                    //check if number doesnot occur before then set its count to 1
                    if(!ac[a]) ac[a] = 1;
                    //if number is already in object increase its count
                    else ac[a]++;
                    return ac;
                    ,)
                    //Using reduce on all the keys of object means all numbers.
                    let res = Object.keys(obj).reduce((ac,a) =>
                    //check if count of current number 'a' is `1` in the above object then add it into array
                    if(obj[a] === 1) ac.push(+a)
                    return ac;
                    ,[])
                    console.log(res)





                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    //to get the object with count of each number in array.
                    let obj = arr.reduce((ac,a) =>
                    //check if number doesnot occur before then set its count to 1
                    if(!ac[a]) ac[a] = 1;
                    //if number is already in object increase its count
                    else ac[a]++;
                    return ac;
                    ,)
                    //Using reduce on all the keys of object means all numbers.
                    let res = Object.keys(obj).reduce((ac,a) =>
                    //check if count of current number 'a' is `1` in the above object then add it into array
                    if(obj[a] === 1) ac.push(+a)
                    return ac;
                    ,[])
                    console.log(res)






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 6 hours ago

























                    answered 15 hours ago









                    Maheer AliMaheer Ali

                    6,766417




                    6,766417












                    • nice appreciate that, that 2nd one looks crazy. I'm assuming the time complexity for would be less than ideal compared to other results?

                      – mph85
                      15 hours ago











                    • @mph85 Yes its a little complex because it doesnot go through the array again and again instead it just store all the result obj and then filter it.Its better regarding performance

                      – Maheer Ali
                      14 hours ago












                    • could you remind me why we need the spread operator? what happens if we don't have it? @Maheer Ali

                      – mph85
                      14 hours ago












                    • Is it because if we don't have it, it'll just log an object?

                      – mph85
                      14 hours ago











                    • @mph85 No if we will not have it. We will have a Set() inside array. We use it convert Set() to Array

                      – Maheer Ali
                      14 hours ago

















                    • nice appreciate that, that 2nd one looks crazy. I'm assuming the time complexity for would be less than ideal compared to other results?

                      – mph85
                      15 hours ago











                    • @mph85 Yes its a little complex because it doesnot go through the array again and again instead it just store all the result obj and then filter it.Its better regarding performance

                      – Maheer Ali
                      14 hours ago












                    • could you remind me why we need the spread operator? what happens if we don't have it? @Maheer Ali

                      – mph85
                      14 hours ago












                    • Is it because if we don't have it, it'll just log an object?

                      – mph85
                      14 hours ago











                    • @mph85 No if we will not have it. We will have a Set() inside array. We use it convert Set() to Array

                      – Maheer Ali
                      14 hours ago
















                    nice appreciate that, that 2nd one looks crazy. I'm assuming the time complexity for would be less than ideal compared to other results?

                    – mph85
                    15 hours ago





                    nice appreciate that, that 2nd one looks crazy. I'm assuming the time complexity for would be less than ideal compared to other results?

                    – mph85
                    15 hours ago













                    @mph85 Yes its a little complex because it doesnot go through the array again and again instead it just store all the result obj and then filter it.Its better regarding performance

                    – Maheer Ali
                    14 hours ago






                    @mph85 Yes its a little complex because it doesnot go through the array again and again instead it just store all the result obj and then filter it.Its better regarding performance

                    – Maheer Ali
                    14 hours ago














                    could you remind me why we need the spread operator? what happens if we don't have it? @Maheer Ali

                    – mph85
                    14 hours ago






                    could you remind me why we need the spread operator? what happens if we don't have it? @Maheer Ali

                    – mph85
                    14 hours ago














                    Is it because if we don't have it, it'll just log an object?

                    – mph85
                    14 hours ago





                    Is it because if we don't have it, it'll just log an object?

                    – mph85
                    14 hours ago













                    @mph85 No if we will not have it. We will have a Set() inside array. We use it convert Set() to Array

                    – Maheer Ali
                    14 hours ago





                    @mph85 No if we will not have it. We will have a Set() inside array. We use it convert Set() to Array

                    – Maheer Ali
                    14 hours ago











                    4














                    You could sort the array before and filter the array by checking only one side for duplicates or both sides.






                    var array = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10],
                    result1,
                    result2;

                    array.sort((a, b) => a - b);

                    result1 = array.filter((v, i, a) => a[i - 1] !== v);
                    result2 = array.filter((v, i, a) => a[i - 1] !== v && a[i + 1] !== v);

                    console.log(...result1);
                    console.log(...result2)








                    share|improve this answer























                    • thank for that, for the result1 what is going on with the a[i - 1] !== v?

                      – mph85
                      23 mins ago











                    • a is the array, i is the actual index, and v is the actual value. it takes the value from the index before of the actual index and looks if the values are not equal.

                      – Nina Scholz
                      21 mins ago











                    • ah ok, and sorry, it takes the value from the index before of the actual index? Not sure what you mean by that

                      – mph85
                      14 mins ago












                    • for example if you have the value 9 of the array as v, then the value before is 8.

                      – Nina Scholz
                      11 mins ago











                    • ah ok, thank you for that

                      – mph85
                      9 mins ago
















                    4














                    You could sort the array before and filter the array by checking only one side for duplicates or both sides.






                    var array = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10],
                    result1,
                    result2;

                    array.sort((a, b) => a - b);

                    result1 = array.filter((v, i, a) => a[i - 1] !== v);
                    result2 = array.filter((v, i, a) => a[i - 1] !== v && a[i + 1] !== v);

                    console.log(...result1);
                    console.log(...result2)








                    share|improve this answer























                    • thank for that, for the result1 what is going on with the a[i - 1] !== v?

                      – mph85
                      23 mins ago











                    • a is the array, i is the actual index, and v is the actual value. it takes the value from the index before of the actual index and looks if the values are not equal.

                      – Nina Scholz
                      21 mins ago











                    • ah ok, and sorry, it takes the value from the index before of the actual index? Not sure what you mean by that

                      – mph85
                      14 mins ago












                    • for example if you have the value 9 of the array as v, then the value before is 8.

                      – Nina Scholz
                      11 mins ago











                    • ah ok, thank you for that

                      – mph85
                      9 mins ago














                    4












                    4








                    4







                    You could sort the array before and filter the array by checking only one side for duplicates or both sides.






                    var array = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10],
                    result1,
                    result2;

                    array.sort((a, b) => a - b);

                    result1 = array.filter((v, i, a) => a[i - 1] !== v);
                    result2 = array.filter((v, i, a) => a[i - 1] !== v && a[i + 1] !== v);

                    console.log(...result1);
                    console.log(...result2)








                    share|improve this answer













                    You could sort the array before and filter the array by checking only one side for duplicates or both sides.






                    var array = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10],
                    result1,
                    result2;

                    array.sort((a, b) => a - b);

                    result1 = array.filter((v, i, a) => a[i - 1] !== v);
                    result2 = array.filter((v, i, a) => a[i - 1] !== v && a[i + 1] !== v);

                    console.log(...result1);
                    console.log(...result2)








                    var array = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10],
                    result1,
                    result2;

                    array.sort((a, b) => a - b);

                    result1 = array.filter((v, i, a) => a[i - 1] !== v);
                    result2 = array.filter((v, i, a) => a[i - 1] !== v && a[i + 1] !== v);

                    console.log(...result1);
                    console.log(...result2)





                    var array = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10],
                    result1,
                    result2;

                    array.sort((a, b) => a - b);

                    result1 = array.filter((v, i, a) => a[i - 1] !== v);
                    result2 = array.filter((v, i, a) => a[i - 1] !== v && a[i + 1] !== v);

                    console.log(...result1);
                    console.log(...result2)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 14 hours ago









                    Nina ScholzNina Scholz

                    192k15104177




                    192k15104177












                    • thank for that, for the result1 what is going on with the a[i - 1] !== v?

                      – mph85
                      23 mins ago











                    • a is the array, i is the actual index, and v is the actual value. it takes the value from the index before of the actual index and looks if the values are not equal.

                      – Nina Scholz
                      21 mins ago











                    • ah ok, and sorry, it takes the value from the index before of the actual index? Not sure what you mean by that

                      – mph85
                      14 mins ago












                    • for example if you have the value 9 of the array as v, then the value before is 8.

                      – Nina Scholz
                      11 mins ago











                    • ah ok, thank you for that

                      – mph85
                      9 mins ago


















                    • thank for that, for the result1 what is going on with the a[i - 1] !== v?

                      – mph85
                      23 mins ago











                    • a is the array, i is the actual index, and v is the actual value. it takes the value from the index before of the actual index and looks if the values are not equal.

                      – Nina Scholz
                      21 mins ago











                    • ah ok, and sorry, it takes the value from the index before of the actual index? Not sure what you mean by that

                      – mph85
                      14 mins ago












                    • for example if you have the value 9 of the array as v, then the value before is 8.

                      – Nina Scholz
                      11 mins ago











                    • ah ok, thank you for that

                      – mph85
                      9 mins ago

















                    thank for that, for the result1 what is going on with the a[i - 1] !== v?

                    – mph85
                    23 mins ago





                    thank for that, for the result1 what is going on with the a[i - 1] !== v?

                    – mph85
                    23 mins ago













                    a is the array, i is the actual index, and v is the actual value. it takes the value from the index before of the actual index and looks if the values are not equal.

                    – Nina Scholz
                    21 mins ago





                    a is the array, i is the actual index, and v is the actual value. it takes the value from the index before of the actual index and looks if the values are not equal.

                    – Nina Scholz
                    21 mins ago













                    ah ok, and sorry, it takes the value from the index before of the actual index? Not sure what you mean by that

                    – mph85
                    14 mins ago






                    ah ok, and sorry, it takes the value from the index before of the actual index? Not sure what you mean by that

                    – mph85
                    14 mins ago














                    for example if you have the value 9 of the array as v, then the value before is 8.

                    – Nina Scholz
                    11 mins ago





                    for example if you have the value 9 of the array as v, then the value before is 8.

                    – Nina Scholz
                    11 mins ago













                    ah ok, thank you for that

                    – mph85
                    9 mins ago






                    ah ok, thank you for that

                    – mph85
                    9 mins ago












                    4














                    You can create both arrays in One Go






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let unique = new Set();
                    let repeated = Array.from(arr.reduce((acc, curr) =>
                    acc.has(curr) ? unique.delete(curr) : acc.add(curr) && unique.add(curr);
                    return acc;
                    , new Set()));

                    console.log(Array.from(unique))
                    console.log(repeated)








                    share|improve this answer


















                    • 1





                      Nice one mate +1

                      – Maheer Ali
                      14 hours ago











                    • I wonder if the ternary plus && could be unclear though (x?y:z&&w)? It's not obvious to me how JS's order of operations would handle that, and I wonder if you could get across the same logic and reasoning by using if/else?

                      – Florrie
                      7 hours ago















                    4














                    You can create both arrays in One Go






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let unique = new Set();
                    let repeated = Array.from(arr.reduce((acc, curr) =>
                    acc.has(curr) ? unique.delete(curr) : acc.add(curr) && unique.add(curr);
                    return acc;
                    , new Set()));

                    console.log(Array.from(unique))
                    console.log(repeated)








                    share|improve this answer


















                    • 1





                      Nice one mate +1

                      – Maheer Ali
                      14 hours ago











                    • I wonder if the ternary plus && could be unclear though (x?y:z&&w)? It's not obvious to me how JS's order of operations would handle that, and I wonder if you could get across the same logic and reasoning by using if/else?

                      – Florrie
                      7 hours ago













                    4












                    4








                    4







                    You can create both arrays in One Go






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let unique = new Set();
                    let repeated = Array.from(arr.reduce((acc, curr) =>
                    acc.has(curr) ? unique.delete(curr) : acc.add(curr) && unique.add(curr);
                    return acc;
                    , new Set()));

                    console.log(Array.from(unique))
                    console.log(repeated)








                    share|improve this answer













                    You can create both arrays in One Go






                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let unique = new Set();
                    let repeated = Array.from(arr.reduce((acc, curr) =>
                    acc.has(curr) ? unique.delete(curr) : acc.add(curr) && unique.add(curr);
                    return acc;
                    , new Set()));

                    console.log(Array.from(unique))
                    console.log(repeated)








                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let unique = new Set();
                    let repeated = Array.from(arr.reduce((acc, curr) =>
                    acc.has(curr) ? unique.delete(curr) : acc.add(curr) && unique.add(curr);
                    return acc;
                    , new Set()));

                    console.log(Array.from(unique))
                    console.log(repeated)





                    let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    let unique = new Set();
                    let repeated = Array.from(arr.reduce((acc, curr) =>
                    acc.has(curr) ? unique.delete(curr) : acc.add(curr) && unique.add(curr);
                    return acc;
                    , new Set()));

                    console.log(Array.from(unique))
                    console.log(repeated)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 14 hours ago









                    AZ_AZ_

                    559210




                    559210







                    • 1





                      Nice one mate +1

                      – Maheer Ali
                      14 hours ago











                    • I wonder if the ternary plus && could be unclear though (x?y:z&&w)? It's not obvious to me how JS's order of operations would handle that, and I wonder if you could get across the same logic and reasoning by using if/else?

                      – Florrie
                      7 hours ago












                    • 1





                      Nice one mate +1

                      – Maheer Ali
                      14 hours ago











                    • I wonder if the ternary plus && could be unclear though (x?y:z&&w)? It's not obvious to me how JS's order of operations would handle that, and I wonder if you could get across the same logic and reasoning by using if/else?

                      – Florrie
                      7 hours ago







                    1




                    1





                    Nice one mate +1

                    – Maheer Ali
                    14 hours ago





                    Nice one mate +1

                    – Maheer Ali
                    14 hours ago













                    I wonder if the ternary plus && could be unclear though (x?y:z&&w)? It's not obvious to me how JS's order of operations would handle that, and I wonder if you could get across the same logic and reasoning by using if/else?

                    – Florrie
                    7 hours ago





                    I wonder if the ternary plus && could be unclear though (x?y:z&&w)? It's not obvious to me how JS's order of operations would handle that, and I wonder if you could get across the same logic and reasoning by using if/else?

                    – Florrie
                    7 hours ago











                    4














                    You can do:






                    const arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                    const hash = arr.reduce((a, c) => (a[c] = (a[c] || 0) + 1, a), );

                    // [1, 2, 3, 4, 5, 8, 9, 10];
                    const finalResultOne = Object.keys(hash);

                    // [1, 9, 10];
                    const finalResultTwo = Object.keys(hash).filter(k => hash[k] === 1);

                    console.log('finalResultOne:', ...finalResultOne);
                    console.log('finalResultTwo:', ...finalResultTwo);








                    share|improve this answer





























                      4














                      You can do:






                      const arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                      const hash = arr.reduce((a, c) => (a[c] = (a[c] || 0) + 1, a), );

                      // [1, 2, 3, 4, 5, 8, 9, 10];
                      const finalResultOne = Object.keys(hash);

                      // [1, 9, 10];
                      const finalResultTwo = Object.keys(hash).filter(k => hash[k] === 1);

                      console.log('finalResultOne:', ...finalResultOne);
                      console.log('finalResultTwo:', ...finalResultTwo);








                      share|improve this answer



























                        4












                        4








                        4







                        You can do:






                        const arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                        const hash = arr.reduce((a, c) => (a[c] = (a[c] || 0) + 1, a), );

                        // [1, 2, 3, 4, 5, 8, 9, 10];
                        const finalResultOne = Object.keys(hash);

                        // [1, 9, 10];
                        const finalResultTwo = Object.keys(hash).filter(k => hash[k] === 1);

                        console.log('finalResultOne:', ...finalResultOne);
                        console.log('finalResultTwo:', ...finalResultTwo);








                        share|improve this answer















                        You can do:






                        const arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                        const hash = arr.reduce((a, c) => (a[c] = (a[c] || 0) + 1, a), );

                        // [1, 2, 3, 4, 5, 8, 9, 10];
                        const finalResultOne = Object.keys(hash);

                        // [1, 9, 10];
                        const finalResultTwo = Object.keys(hash).filter(k => hash[k] === 1);

                        console.log('finalResultOne:', ...finalResultOne);
                        console.log('finalResultTwo:', ...finalResultTwo);








                        const arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                        const hash = arr.reduce((a, c) => (a[c] = (a[c] || 0) + 1, a), );

                        // [1, 2, 3, 4, 5, 8, 9, 10];
                        const finalResultOne = Object.keys(hash);

                        // [1, 9, 10];
                        const finalResultTwo = Object.keys(hash).filter(k => hash[k] === 1);

                        console.log('finalResultOne:', ...finalResultOne);
                        console.log('finalResultTwo:', ...finalResultTwo);





                        const arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                        const hash = arr.reduce((a, c) => (a[c] = (a[c] || 0) + 1, a), );

                        // [1, 2, 3, 4, 5, 8, 9, 10];
                        const finalResultOne = Object.keys(hash);

                        // [1, 9, 10];
                        const finalResultTwo = Object.keys(hash).filter(k => hash[k] === 1);

                        console.log('finalResultOne:', ...finalResultOne);
                        console.log('finalResultTwo:', ...finalResultTwo);






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited 10 hours ago

























                        answered 14 hours ago









                        Yosvel QuinteroYosvel Quintero

                        11.8k42531




                        11.8k42531





















                            3














                            You can use closure and Map






                            let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                            const build = ar =>
                            const mapObj = ar.reduce((acc, e) =>
                            acc.has(e) ? acc.set(e, true) : acc.set(e, false)
                            return acc
                            , new Map())

                            return function(hasDup = true)
                            if(hasDup) return [...mapObj.keys()]
                            else return [...mapObj].filter(([key, val]) => !val).map(([k, v])=> k)



                            const getArr = build(arr)

                            console.log(getArr())
                            console.log(getArr(false))








                            share|improve this answer



























                              3














                              You can use closure and Map






                              let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                              const build = ar =>
                              const mapObj = ar.reduce((acc, e) =>
                              acc.has(e) ? acc.set(e, true) : acc.set(e, false)
                              return acc
                              , new Map())

                              return function(hasDup = true)
                              if(hasDup) return [...mapObj.keys()]
                              else return [...mapObj].filter(([key, val]) => !val).map(([k, v])=> k)



                              const getArr = build(arr)

                              console.log(getArr())
                              console.log(getArr(false))








                              share|improve this answer

























                                3












                                3








                                3







                                You can use closure and Map






                                let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                                const build = ar =>
                                const mapObj = ar.reduce((acc, e) =>
                                acc.has(e) ? acc.set(e, true) : acc.set(e, false)
                                return acc
                                , new Map())

                                return function(hasDup = true)
                                if(hasDup) return [...mapObj.keys()]
                                else return [...mapObj].filter(([key, val]) => !val).map(([k, v])=> k)



                                const getArr = build(arr)

                                console.log(getArr())
                                console.log(getArr(false))








                                share|improve this answer













                                You can use closure and Map






                                let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                                const build = ar =>
                                const mapObj = ar.reduce((acc, e) =>
                                acc.has(e) ? acc.set(e, true) : acc.set(e, false)
                                return acc
                                , new Map())

                                return function(hasDup = true)
                                if(hasDup) return [...mapObj.keys()]
                                else return [...mapObj].filter(([key, val]) => !val).map(([k, v])=> k)



                                const getArr = build(arr)

                                console.log(getArr())
                                console.log(getArr(false))








                                let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                                const build = ar =>
                                const mapObj = ar.reduce((acc, e) =>
                                acc.has(e) ? acc.set(e, true) : acc.set(e, false)
                                return acc
                                , new Map())

                                return function(hasDup = true)
                                if(hasDup) return [...mapObj.keys()]
                                else return [...mapObj].filter(([key, val]) => !val).map(([k, v])=> k)



                                const getArr = build(arr)

                                console.log(getArr())
                                console.log(getArr(false))





                                let arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                                const build = ar =>
                                const mapObj = ar.reduce((acc, e) =>
                                acc.has(e) ? acc.set(e, true) : acc.set(e, false)
                                return acc
                                , new Map())

                                return function(hasDup = true)
                                if(hasDup) return [...mapObj.keys()]
                                else return [...mapObj].filter(([key, val]) => !val).map(([k, v])=> k)



                                const getArr = build(arr)

                                console.log(getArr())
                                console.log(getArr(false))






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered 14 hours ago









                                birdbird

                                822619




                                822619





















                                    3














                                    As many other have said, the first one is just [...new Set(arr)]



                                    For the second, just filter out those that occur more than once:




                                    const arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                                    const count = (arr, e) => arr.filter(n => n == e).length

                                    const unique = arr => arr.filter(e => count(arr, e) < 2)

                                    console.log(unique(arr));








                                    share|improve this answer



























                                      3














                                      As many other have said, the first one is just [...new Set(arr)]



                                      For the second, just filter out those that occur more than once:




                                      const arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                                      const count = (arr, e) => arr.filter(n => n == e).length

                                      const unique = arr => arr.filter(e => count(arr, e) < 2)

                                      console.log(unique(arr));








                                      share|improve this answer

























                                        3












                                        3








                                        3







                                        As many other have said, the first one is just [...new Set(arr)]



                                        For the second, just filter out those that occur more than once:




                                        const arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                                        const count = (arr, e) => arr.filter(n => n == e).length

                                        const unique = arr => arr.filter(e => count(arr, e) < 2)

                                        console.log(unique(arr));








                                        share|improve this answer













                                        As many other have said, the first one is just [...new Set(arr)]



                                        For the second, just filter out those that occur more than once:




                                        const arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                                        const count = (arr, e) => arr.filter(n => n == e).length

                                        const unique = arr => arr.filter(e => count(arr, e) < 2)

                                        console.log(unique(arr));








                                        const arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                                        const count = (arr, e) => arr.filter(n => n == e).length

                                        const unique = arr => arr.filter(e => count(arr, e) < 2)

                                        console.log(unique(arr));





                                        const arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];

                                        const count = (arr, e) => arr.filter(n => n == e).length

                                        const unique = arr => arr.filter(e => count(arr, e) < 2)

                                        console.log(unique(arr));






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered 10 hours ago









                                        JollyJokerJollyJoker

                                        20115




                                        20115





















                                            0














                                            var arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                                            var map = ;
                                            var finalResult = [];
                                            for(var i = 0; i < arr.length; i++)
                                            if(!map.hasOwnProperty(arr[i]))
                                            map[arr[i]] = true;
                                            finalResult.push(arr[i]);



                                            //if you need it sorted otherwise it will be in order
                                            finalResult.sort(function(a, b)return a-b);





                                            share|improve this answer



























                                              0














                                              var arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                                              var map = ;
                                              var finalResult = [];
                                              for(var i = 0; i < arr.length; i++)
                                              if(!map.hasOwnProperty(arr[i]))
                                              map[arr[i]] = true;
                                              finalResult.push(arr[i]);



                                              //if you need it sorted otherwise it will be in order
                                              finalResult.sort(function(a, b)return a-b);





                                              share|improve this answer

























                                                0












                                                0








                                                0







                                                var arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                                                var map = ;
                                                var finalResult = [];
                                                for(var i = 0; i < arr.length; i++)
                                                if(!map.hasOwnProperty(arr[i]))
                                                map[arr[i]] = true;
                                                finalResult.push(arr[i]);



                                                //if you need it sorted otherwise it will be in order
                                                finalResult.sort(function(a, b)return a-b);





                                                share|improve this answer













                                                var arr = [1, 2, 4, 2, 3, 3, 4, 5, 5, 5, 8, 8, 9, 10];
                                                var map = ;
                                                var finalResult = [];
                                                for(var i = 0; i < arr.length; i++)
                                                if(!map.hasOwnProperty(arr[i]))
                                                map[arr[i]] = true;
                                                finalResult.push(arr[i]);



                                                //if you need it sorted otherwise it will be in order
                                                finalResult.sort(function(a, b)return a-b);






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered 8 hours ago









                                                JanspeedJanspeed

                                                1,3031315




                                                1,3031315













                                                    Popular posts from this blog

                                                    How to create a command for the “strange m” symbol in latex? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)How do you make your own symbol when Detexify fails?Writing bold small caps with mathpazo packageplus-minus symbol with parenthesis around the minus signGreek character in Beamer document titleHow to create dashed right arrow over symbol?Currency symbol: Turkish LiraDouble prec as a single symbol?Plus Sign Too Big; How to Call adfbullet?Is there a TeX macro for three-legged pi?How do I get my integral-like symbol to align like the integral?How to selectively substitute a letter with another symbol representing the same letterHow do I generate a less than symbol and vertical bar that are the same height?

                                                    Category:Tremithousa Media in category "Tremithousa"Navigation menuUpload media34° 49′ 02.7″ N, 32° 26′ 37.32″ EOpenStreetMapGoogle EarthProximityramaReasonatorScholiaStatisticsWikiShootMe

                                                    Dokschytsy (Steed) Kwelen | NawigatsjuunBelarus: Vitebsk Region, citypopulation.de