Remove Only One Duplicate from An Array












2















I'm trying to only remove one of the 2s from an array, but my code removes all of them. My code is as follows:



var arr = [2,7,9,5,2]
arr.filter(item => ((item !== 2)));


and:



var arr = [2,7,9,2,2,5,2]
arr.filter(item => ((item !== 2)));


Both remove all the 2s. I thought about removing duplicates, where it works if there's only one duplicate - e.g.:



Array.from(new Set([2,7,9,5,2]));
function uniq(a) {
return Array.from(new Set(a))
}


But fails if there's multiple duplicates as it just removes them all, including any other duplicated numbers:



Array.from(new Set([2,7,9,9,2,2,5,2]));
function uniq(a) {
return Array.from(new Set(a))
}


Does anyone know how to only remove one of the 2s? Thanks for any help here.










share|improve this question


















  • 1





    create a simple loop, when found an index with value of 2: .splice(idx, 1) this index and break the loop

    – Calvin Nunes
    Nov 19 '18 at 10:34








  • 1





    Which one do you want to remove ? The first 2, or last, or ...

    – Mihai Alexandru-Ionut
    Nov 19 '18 at 10:34






  • 1





    Possible duplicate of How do I remove a particular element from an array in JavaScript?

    – Haroldo_OK
    Nov 19 '18 at 10:38
















2















I'm trying to only remove one of the 2s from an array, but my code removes all of them. My code is as follows:



var arr = [2,7,9,5,2]
arr.filter(item => ((item !== 2)));


and:



var arr = [2,7,9,2,2,5,2]
arr.filter(item => ((item !== 2)));


Both remove all the 2s. I thought about removing duplicates, where it works if there's only one duplicate - e.g.:



Array.from(new Set([2,7,9,5,2]));
function uniq(a) {
return Array.from(new Set(a))
}


But fails if there's multiple duplicates as it just removes them all, including any other duplicated numbers:



Array.from(new Set([2,7,9,9,2,2,5,2]));
function uniq(a) {
return Array.from(new Set(a))
}


Does anyone know how to only remove one of the 2s? Thanks for any help here.










share|improve this question


















  • 1





    create a simple loop, when found an index with value of 2: .splice(idx, 1) this index and break the loop

    – Calvin Nunes
    Nov 19 '18 at 10:34








  • 1





    Which one do you want to remove ? The first 2, or last, or ...

    – Mihai Alexandru-Ionut
    Nov 19 '18 at 10:34






  • 1





    Possible duplicate of How do I remove a particular element from an array in JavaScript?

    – Haroldo_OK
    Nov 19 '18 at 10:38














2












2








2








I'm trying to only remove one of the 2s from an array, but my code removes all of them. My code is as follows:



var arr = [2,7,9,5,2]
arr.filter(item => ((item !== 2)));


and:



var arr = [2,7,9,2,2,5,2]
arr.filter(item => ((item !== 2)));


Both remove all the 2s. I thought about removing duplicates, where it works if there's only one duplicate - e.g.:



Array.from(new Set([2,7,9,5,2]));
function uniq(a) {
return Array.from(new Set(a))
}


But fails if there's multiple duplicates as it just removes them all, including any other duplicated numbers:



Array.from(new Set([2,7,9,9,2,2,5,2]));
function uniq(a) {
return Array.from(new Set(a))
}


Does anyone know how to only remove one of the 2s? Thanks for any help here.










share|improve this question














I'm trying to only remove one of the 2s from an array, but my code removes all of them. My code is as follows:



var arr = [2,7,9,5,2]
arr.filter(item => ((item !== 2)));


and:



var arr = [2,7,9,2,2,5,2]
arr.filter(item => ((item !== 2)));


Both remove all the 2s. I thought about removing duplicates, where it works if there's only one duplicate - e.g.:



Array.from(new Set([2,7,9,5,2]));
function uniq(a) {
return Array.from(new Set(a))
}


But fails if there's multiple duplicates as it just removes them all, including any other duplicated numbers:



Array.from(new Set([2,7,9,9,2,2,5,2]));
function uniq(a) {
return Array.from(new Set(a))
}


Does anyone know how to only remove one of the 2s? Thanks for any help here.







javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 10:32









user8758206user8758206

456110




456110








  • 1





    create a simple loop, when found an index with value of 2: .splice(idx, 1) this index and break the loop

    – Calvin Nunes
    Nov 19 '18 at 10:34








  • 1





    Which one do you want to remove ? The first 2, or last, or ...

    – Mihai Alexandru-Ionut
    Nov 19 '18 at 10:34






  • 1





    Possible duplicate of How do I remove a particular element from an array in JavaScript?

    – Haroldo_OK
    Nov 19 '18 at 10:38














  • 1





    create a simple loop, when found an index with value of 2: .splice(idx, 1) this index and break the loop

    – Calvin Nunes
    Nov 19 '18 at 10:34








  • 1





    Which one do you want to remove ? The first 2, or last, or ...

    – Mihai Alexandru-Ionut
    Nov 19 '18 at 10:34






  • 1





    Possible duplicate of How do I remove a particular element from an array in JavaScript?

    – Haroldo_OK
    Nov 19 '18 at 10:38








1




1





create a simple loop, when found an index with value of 2: .splice(idx, 1) this index and break the loop

– Calvin Nunes
Nov 19 '18 at 10:34







create a simple loop, when found an index with value of 2: .splice(idx, 1) this index and break the loop

– Calvin Nunes
Nov 19 '18 at 10:34






1




1





Which one do you want to remove ? The first 2, or last, or ...

– Mihai Alexandru-Ionut
Nov 19 '18 at 10:34





Which one do you want to remove ? The first 2, or last, or ...

– Mihai Alexandru-Ionut
Nov 19 '18 at 10:34




1




1





Possible duplicate of How do I remove a particular element from an array in JavaScript?

– Haroldo_OK
Nov 19 '18 at 10:38





Possible duplicate of How do I remove a particular element from an array in JavaScript?

– Haroldo_OK
Nov 19 '18 at 10:38












6 Answers
6






active

oldest

votes


















3














You could use indexOf method in combination with splice.






var arr = [2,7,9,5,2]
var idx = arr.indexOf(2)
if (idx >= 0) {
arr.splice(idx, 1);
}
console.log(arr);








share|improve this answer


























  • Close, but it won't work correctly if the number isn't present on the array; checking if indexOf is non-negative would make it more robust.

    – Haroldo_OK
    Nov 19 '18 at 10:47






  • 1





    brilliant, thank you

    – user8758206
    Nov 19 '18 at 12:05



















2














You could take a closure with a counter and remove only the first 2.






var array = [2, 7, 9, 2, 3, 2, 5, 2],
result = array.filter((i => v => v !== 2 || --i)(1));

console.log(result);





For any other 2, you could adjust the start value for decrementing.






var array = [2, 7, 9, 2, 3, 2, 5, 2],
result = array.filter((i => v => v !== 2 || --i)(2));

console.log(result);








share|improve this answer


























  • interesting answer. What does the v and (1) and (2) represent? I'm new to JS and haven't seen this type of syntax before - can you link to anything I can read to learn this?

    – user8758206
    Nov 19 '18 at 16:44






  • 1





    one is to remove the first occurence of 2 and two for the second, and so on. it works, because the number gets decremented and if zero, the value is filterd out. the technique's name is closure with an IIFE (immediately-invoked function expression).

    – Nina Scholz
    Nov 19 '18 at 17:00











  • you're obviously a very advanced coder - thank you

    – user8758206
    Nov 19 '18 at 20:41



















1














you can follow the following method



var arr= [2,3,4,2,4,5];
var unique = ;
$.each(arr, function(i, el){
if($.inArray(el, unique) === -1) unique.push(el);
})





share|improve this answer































    1














    There are various ways to do that; one relatively simple way would be to use indexOf; see this other post: https://stackoverflow.com/a/5767357/679240






    var array = [2, 7, 9, 5, 2];
    console.log(array)
    var index = array.indexOf(2);
    if (index > -1) {
    array.splice(index, 1);
    }
    // array = [7, 9, 5, 2]
    console.log(array);








    share|improve this answer

































      1














      You can do:






      const arr = [2, 7, 9, 2, 2, 5, 2];
      const result = arr
      .reduce((a, c) => {
      a.temp[c] = ++a.temp[c] || 1;
      if (a.temp[c] !== 2) {
      a.array.push(c);
      }
      return a;
      }, {temp: {}, array: })
      .array;

      console.log(result);








      share|improve this answer

































        1














        Most simple way to filter all duplicates from array:



        arr.filter((item, position) => arr.indexOf(item) === position)


        This method skip element if another element with the same value already exist.



        If you need to filter only first duplicate, you can use additional bool key:



        arr.filter((item, position) => {
        if (!already && arr.indexOf(item) !== position) {
        already = true
        return false
        } else return true
        })


        But this method have overheaded. Smartest way is use for loop:



        for (let i = 0; i < arr.length; i++) {
        if (arr.indexOf(arr[i]) !== i) {
        arr.splice(i,1);
        break;
        }
        }





        share|improve this answer























          Your Answer






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

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

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

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


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372690%2fremove-only-one-duplicate-from-an-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          You could use indexOf method in combination with splice.






          var arr = [2,7,9,5,2]
          var idx = arr.indexOf(2)
          if (idx >= 0) {
          arr.splice(idx, 1);
          }
          console.log(arr);








          share|improve this answer


























          • Close, but it won't work correctly if the number isn't present on the array; checking if indexOf is non-negative would make it more robust.

            – Haroldo_OK
            Nov 19 '18 at 10:47






          • 1





            brilliant, thank you

            – user8758206
            Nov 19 '18 at 12:05
















          3














          You could use indexOf method in combination with splice.






          var arr = [2,7,9,5,2]
          var idx = arr.indexOf(2)
          if (idx >= 0) {
          arr.splice(idx, 1);
          }
          console.log(arr);








          share|improve this answer


























          • Close, but it won't work correctly if the number isn't present on the array; checking if indexOf is non-negative would make it more robust.

            – Haroldo_OK
            Nov 19 '18 at 10:47






          • 1





            brilliant, thank you

            – user8758206
            Nov 19 '18 at 12:05














          3












          3








          3







          You could use indexOf method in combination with splice.






          var arr = [2,7,9,5,2]
          var idx = arr.indexOf(2)
          if (idx >= 0) {
          arr.splice(idx, 1);
          }
          console.log(arr);








          share|improve this answer















          You could use indexOf method in combination with splice.






          var arr = [2,7,9,5,2]
          var idx = arr.indexOf(2)
          if (idx >= 0) {
          arr.splice(idx, 1);
          }
          console.log(arr);








          var arr = [2,7,9,5,2]
          var idx = arr.indexOf(2)
          if (idx >= 0) {
          arr.splice(idx, 1);
          }
          console.log(arr);





          var arr = [2,7,9,5,2]
          var idx = arr.indexOf(2)
          if (idx >= 0) {
          arr.splice(idx, 1);
          }
          console.log(arr);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 '18 at 10:49









          Haroldo_OK

          3,32521847




          3,32521847










          answered Nov 19 '18 at 10:39









          Mihai Alexandru-IonutMihai Alexandru-Ionut

          30.3k63971




          30.3k63971













          • Close, but it won't work correctly if the number isn't present on the array; checking if indexOf is non-negative would make it more robust.

            – Haroldo_OK
            Nov 19 '18 at 10:47






          • 1





            brilliant, thank you

            – user8758206
            Nov 19 '18 at 12:05



















          • Close, but it won't work correctly if the number isn't present on the array; checking if indexOf is non-negative would make it more robust.

            – Haroldo_OK
            Nov 19 '18 at 10:47






          • 1





            brilliant, thank you

            – user8758206
            Nov 19 '18 at 12:05

















          Close, but it won't work correctly if the number isn't present on the array; checking if indexOf is non-negative would make it more robust.

          – Haroldo_OK
          Nov 19 '18 at 10:47





          Close, but it won't work correctly if the number isn't present on the array; checking if indexOf is non-negative would make it more robust.

          – Haroldo_OK
          Nov 19 '18 at 10:47




          1




          1





          brilliant, thank you

          – user8758206
          Nov 19 '18 at 12:05





          brilliant, thank you

          – user8758206
          Nov 19 '18 at 12:05













          2














          You could take a closure with a counter and remove only the first 2.






          var array = [2, 7, 9, 2, 3, 2, 5, 2],
          result = array.filter((i => v => v !== 2 || --i)(1));

          console.log(result);





          For any other 2, you could adjust the start value for decrementing.






          var array = [2, 7, 9, 2, 3, 2, 5, 2],
          result = array.filter((i => v => v !== 2 || --i)(2));

          console.log(result);








          share|improve this answer


























          • interesting answer. What does the v and (1) and (2) represent? I'm new to JS and haven't seen this type of syntax before - can you link to anything I can read to learn this?

            – user8758206
            Nov 19 '18 at 16:44






          • 1





            one is to remove the first occurence of 2 and two for the second, and so on. it works, because the number gets decremented and if zero, the value is filterd out. the technique's name is closure with an IIFE (immediately-invoked function expression).

            – Nina Scholz
            Nov 19 '18 at 17:00











          • you're obviously a very advanced coder - thank you

            – user8758206
            Nov 19 '18 at 20:41
















          2














          You could take a closure with a counter and remove only the first 2.






          var array = [2, 7, 9, 2, 3, 2, 5, 2],
          result = array.filter((i => v => v !== 2 || --i)(1));

          console.log(result);





          For any other 2, you could adjust the start value for decrementing.






          var array = [2, 7, 9, 2, 3, 2, 5, 2],
          result = array.filter((i => v => v !== 2 || --i)(2));

          console.log(result);








          share|improve this answer


























          • interesting answer. What does the v and (1) and (2) represent? I'm new to JS and haven't seen this type of syntax before - can you link to anything I can read to learn this?

            – user8758206
            Nov 19 '18 at 16:44






          • 1





            one is to remove the first occurence of 2 and two for the second, and so on. it works, because the number gets decremented and if zero, the value is filterd out. the technique's name is closure with an IIFE (immediately-invoked function expression).

            – Nina Scholz
            Nov 19 '18 at 17:00











          • you're obviously a very advanced coder - thank you

            – user8758206
            Nov 19 '18 at 20:41














          2












          2








          2







          You could take a closure with a counter and remove only the first 2.






          var array = [2, 7, 9, 2, 3, 2, 5, 2],
          result = array.filter((i => v => v !== 2 || --i)(1));

          console.log(result);





          For any other 2, you could adjust the start value for decrementing.






          var array = [2, 7, 9, 2, 3, 2, 5, 2],
          result = array.filter((i => v => v !== 2 || --i)(2));

          console.log(result);








          share|improve this answer















          You could take a closure with a counter and remove only the first 2.






          var array = [2, 7, 9, 2, 3, 2, 5, 2],
          result = array.filter((i => v => v !== 2 || --i)(1));

          console.log(result);





          For any other 2, you could adjust the start value for decrementing.






          var array = [2, 7, 9, 2, 3, 2, 5, 2],
          result = array.filter((i => v => v !== 2 || --i)(2));

          console.log(result);








          var array = [2, 7, 9, 2, 3, 2, 5, 2],
          result = array.filter((i => v => v !== 2 || --i)(1));

          console.log(result);





          var array = [2, 7, 9, 2, 3, 2, 5, 2],
          result = array.filter((i => v => v !== 2 || --i)(1));

          console.log(result);





          var array = [2, 7, 9, 2, 3, 2, 5, 2],
          result = array.filter((i => v => v !== 2 || --i)(2));

          console.log(result);





          var array = [2, 7, 9, 2, 3, 2, 5, 2],
          result = array.filter((i => v => v !== 2 || --i)(2));

          console.log(result);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 '18 at 10:48

























          answered Nov 19 '18 at 10:42









          Nina ScholzNina Scholz

          182k1495164




          182k1495164













          • interesting answer. What does the v and (1) and (2) represent? I'm new to JS and haven't seen this type of syntax before - can you link to anything I can read to learn this?

            – user8758206
            Nov 19 '18 at 16:44






          • 1





            one is to remove the first occurence of 2 and two for the second, and so on. it works, because the number gets decremented and if zero, the value is filterd out. the technique's name is closure with an IIFE (immediately-invoked function expression).

            – Nina Scholz
            Nov 19 '18 at 17:00











          • you're obviously a very advanced coder - thank you

            – user8758206
            Nov 19 '18 at 20:41



















          • interesting answer. What does the v and (1) and (2) represent? I'm new to JS and haven't seen this type of syntax before - can you link to anything I can read to learn this?

            – user8758206
            Nov 19 '18 at 16:44






          • 1





            one is to remove the first occurence of 2 and two for the second, and so on. it works, because the number gets decremented and if zero, the value is filterd out. the technique's name is closure with an IIFE (immediately-invoked function expression).

            – Nina Scholz
            Nov 19 '18 at 17:00











          • you're obviously a very advanced coder - thank you

            – user8758206
            Nov 19 '18 at 20:41

















          interesting answer. What does the v and (1) and (2) represent? I'm new to JS and haven't seen this type of syntax before - can you link to anything I can read to learn this?

          – user8758206
          Nov 19 '18 at 16:44





          interesting answer. What does the v and (1) and (2) represent? I'm new to JS and haven't seen this type of syntax before - can you link to anything I can read to learn this?

          – user8758206
          Nov 19 '18 at 16:44




          1




          1





          one is to remove the first occurence of 2 and two for the second, and so on. it works, because the number gets decremented and if zero, the value is filterd out. the technique's name is closure with an IIFE (immediately-invoked function expression).

          – Nina Scholz
          Nov 19 '18 at 17:00





          one is to remove the first occurence of 2 and two for the second, and so on. it works, because the number gets decremented and if zero, the value is filterd out. the technique's name is closure with an IIFE (immediately-invoked function expression).

          – Nina Scholz
          Nov 19 '18 at 17:00













          you're obviously a very advanced coder - thank you

          – user8758206
          Nov 19 '18 at 20:41





          you're obviously a very advanced coder - thank you

          – user8758206
          Nov 19 '18 at 20:41











          1














          you can follow the following method



          var arr= [2,3,4,2,4,5];
          var unique = ;
          $.each(arr, function(i, el){
          if($.inArray(el, unique) === -1) unique.push(el);
          })





          share|improve this answer




























            1














            you can follow the following method



            var arr= [2,3,4,2,4,5];
            var unique = ;
            $.each(arr, function(i, el){
            if($.inArray(el, unique) === -1) unique.push(el);
            })





            share|improve this answer


























              1












              1








              1







              you can follow the following method



              var arr= [2,3,4,2,4,5];
              var unique = ;
              $.each(arr, function(i, el){
              if($.inArray(el, unique) === -1) unique.push(el);
              })





              share|improve this answer













              you can follow the following method



              var arr= [2,3,4,2,4,5];
              var unique = ;
              $.each(arr, function(i, el){
              if($.inArray(el, unique) === -1) unique.push(el);
              })






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 19 '18 at 10:42









              Tijo JohnTijo John

              372516




              372516























                  1














                  There are various ways to do that; one relatively simple way would be to use indexOf; see this other post: https://stackoverflow.com/a/5767357/679240






                  var array = [2, 7, 9, 5, 2];
                  console.log(array)
                  var index = array.indexOf(2);
                  if (index > -1) {
                  array.splice(index, 1);
                  }
                  // array = [7, 9, 5, 2]
                  console.log(array);








                  share|improve this answer






























                    1














                    There are various ways to do that; one relatively simple way would be to use indexOf; see this other post: https://stackoverflow.com/a/5767357/679240






                    var array = [2, 7, 9, 5, 2];
                    console.log(array)
                    var index = array.indexOf(2);
                    if (index > -1) {
                    array.splice(index, 1);
                    }
                    // array = [7, 9, 5, 2]
                    console.log(array);








                    share|improve this answer




























                      1












                      1








                      1







                      There are various ways to do that; one relatively simple way would be to use indexOf; see this other post: https://stackoverflow.com/a/5767357/679240






                      var array = [2, 7, 9, 5, 2];
                      console.log(array)
                      var index = array.indexOf(2);
                      if (index > -1) {
                      array.splice(index, 1);
                      }
                      // array = [7, 9, 5, 2]
                      console.log(array);








                      share|improve this answer















                      There are various ways to do that; one relatively simple way would be to use indexOf; see this other post: https://stackoverflow.com/a/5767357/679240






                      var array = [2, 7, 9, 5, 2];
                      console.log(array)
                      var index = array.indexOf(2);
                      if (index > -1) {
                      array.splice(index, 1);
                      }
                      // array = [7, 9, 5, 2]
                      console.log(array);








                      var array = [2, 7, 9, 5, 2];
                      console.log(array)
                      var index = array.indexOf(2);
                      if (index > -1) {
                      array.splice(index, 1);
                      }
                      // array = [7, 9, 5, 2]
                      console.log(array);





                      var array = [2, 7, 9, 5, 2];
                      console.log(array)
                      var index = array.indexOf(2);
                      if (index > -1) {
                      array.splice(index, 1);
                      }
                      // array = [7, 9, 5, 2]
                      console.log(array);






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 19 '18 at 10:43

























                      answered Nov 19 '18 at 10:38









                      Haroldo_OKHaroldo_OK

                      3,32521847




                      3,32521847























                          1














                          You can do:






                          const arr = [2, 7, 9, 2, 2, 5, 2];
                          const result = arr
                          .reduce((a, c) => {
                          a.temp[c] = ++a.temp[c] || 1;
                          if (a.temp[c] !== 2) {
                          a.array.push(c);
                          }
                          return a;
                          }, {temp: {}, array: })
                          .array;

                          console.log(result);








                          share|improve this answer






























                            1














                            You can do:






                            const arr = [2, 7, 9, 2, 2, 5, 2];
                            const result = arr
                            .reduce((a, c) => {
                            a.temp[c] = ++a.temp[c] || 1;
                            if (a.temp[c] !== 2) {
                            a.array.push(c);
                            }
                            return a;
                            }, {temp: {}, array: })
                            .array;

                            console.log(result);








                            share|improve this answer




























                              1












                              1








                              1







                              You can do:






                              const arr = [2, 7, 9, 2, 2, 5, 2];
                              const result = arr
                              .reduce((a, c) => {
                              a.temp[c] = ++a.temp[c] || 1;
                              if (a.temp[c] !== 2) {
                              a.array.push(c);
                              }
                              return a;
                              }, {temp: {}, array: })
                              .array;

                              console.log(result);








                              share|improve this answer















                              You can do:






                              const arr = [2, 7, 9, 2, 2, 5, 2];
                              const result = arr
                              .reduce((a, c) => {
                              a.temp[c] = ++a.temp[c] || 1;
                              if (a.temp[c] !== 2) {
                              a.array.push(c);
                              }
                              return a;
                              }, {temp: {}, array: })
                              .array;

                              console.log(result);








                              const arr = [2, 7, 9, 2, 2, 5, 2];
                              const result = arr
                              .reduce((a, c) => {
                              a.temp[c] = ++a.temp[c] || 1;
                              if (a.temp[c] !== 2) {
                              a.array.push(c);
                              }
                              return a;
                              }, {temp: {}, array: })
                              .array;

                              console.log(result);





                              const arr = [2, 7, 9, 2, 2, 5, 2];
                              const result = arr
                              .reduce((a, c) => {
                              a.temp[c] = ++a.temp[c] || 1;
                              if (a.temp[c] !== 2) {
                              a.array.push(c);
                              }
                              return a;
                              }, {temp: {}, array: })
                              .array;

                              console.log(result);






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 19 '18 at 10:46

























                              answered Nov 19 '18 at 10:38









                              Yosvel QuinteroYosvel Quintero

                              11.1k42429




                              11.1k42429























                                  1














                                  Most simple way to filter all duplicates from array:



                                  arr.filter((item, position) => arr.indexOf(item) === position)


                                  This method skip element if another element with the same value already exist.



                                  If you need to filter only first duplicate, you can use additional bool key:



                                  arr.filter((item, position) => {
                                  if (!already && arr.indexOf(item) !== position) {
                                  already = true
                                  return false
                                  } else return true
                                  })


                                  But this method have overheaded. Smartest way is use for loop:



                                  for (let i = 0; i < arr.length; i++) {
                                  if (arr.indexOf(arr[i]) !== i) {
                                  arr.splice(i,1);
                                  break;
                                  }
                                  }





                                  share|improve this answer




























                                    1














                                    Most simple way to filter all duplicates from array:



                                    arr.filter((item, position) => arr.indexOf(item) === position)


                                    This method skip element if another element with the same value already exist.



                                    If you need to filter only first duplicate, you can use additional bool key:



                                    arr.filter((item, position) => {
                                    if (!already && arr.indexOf(item) !== position) {
                                    already = true
                                    return false
                                    } else return true
                                    })


                                    But this method have overheaded. Smartest way is use for loop:



                                    for (let i = 0; i < arr.length; i++) {
                                    if (arr.indexOf(arr[i]) !== i) {
                                    arr.splice(i,1);
                                    break;
                                    }
                                    }





                                    share|improve this answer


























                                      1












                                      1








                                      1







                                      Most simple way to filter all duplicates from array:



                                      arr.filter((item, position) => arr.indexOf(item) === position)


                                      This method skip element if another element with the same value already exist.



                                      If you need to filter only first duplicate, you can use additional bool key:



                                      arr.filter((item, position) => {
                                      if (!already && arr.indexOf(item) !== position) {
                                      already = true
                                      return false
                                      } else return true
                                      })


                                      But this method have overheaded. Smartest way is use for loop:



                                      for (let i = 0; i < arr.length; i++) {
                                      if (arr.indexOf(arr[i]) !== i) {
                                      arr.splice(i,1);
                                      break;
                                      }
                                      }





                                      share|improve this answer













                                      Most simple way to filter all duplicates from array:



                                      arr.filter((item, position) => arr.indexOf(item) === position)


                                      This method skip element if another element with the same value already exist.



                                      If you need to filter only first duplicate, you can use additional bool key:



                                      arr.filter((item, position) => {
                                      if (!already && arr.indexOf(item) !== position) {
                                      already = true
                                      return false
                                      } else return true
                                      })


                                      But this method have overheaded. Smartest way is use for loop:



                                      for (let i = 0; i < arr.length; i++) {
                                      if (arr.indexOf(arr[i]) !== i) {
                                      arr.splice(i,1);
                                      break;
                                      }
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 19 '18 at 11:21









                                      XeelleyXeelley

                                      314




                                      314






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


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

                                          But avoid



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

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


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




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372690%2fremove-only-one-duplicate-from-an-array%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          鏡平學校

                                          ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                                          Why https connections are so slow when debugging (stepping over) in Java?