0
var techologylist=[1,2,2,3,4,4,5]
var newtechologylist=[];
$.each(techologylist, function (i, el) {
   if ($.inArray(el, newtechologylist) === -1) newtechologylist.push(el);
});
console.log(newtechologylist)

I want to remove values from techologylist ,if same 2 value comes .In my example 2 and 4 comes twice ,therefore i want to remove both 2 and 4 from array Am expecting result as [1,3,5]

But result for my code of course give result as [1,2,3,4,5]

How to change my script to remove both values if duplicate occurs

Sreepathy Sp
  • 408
  • 2
  • 6
  • 21
  • I believe I just removed a duplicate from the `questions` array... – Cerbrus Oct 26 '15 at 07:46
  • 1
    I think it is not duplicate.Both cases are different – Sreepathy Sp Oct 26 '15 at 07:49
  • And to attempt to answer the question, I'd have two Objects, one of which keeps track of all values seen, and the other which will contain the output. Loop through the input, if the current value is in the first Object already, delete it from the output Object. If it's not in the first Object, add it to both. – blm Oct 26 '15 at 07:55
  • Right, misleading title much... Reopened. – Cerbrus Oct 26 '15 at 07:56
  • var techologylist=[1,2,2,3,4,4,5]; var newtechologylist=[]; $.each(techologylist, function (i, el) { if ($.inArray(el, newtechologylist) === -1) { newtechologylist.push(el); } else { var index = newtechologylist.indexOf(el); if (index > -1) { newtechologylist.splice(index, 1); } } }); console.log(newtechologylist) – Deepak Biswal Oct 26 '15 at 08:05

1 Answers1

1

var techologylist = [1, 2, 2, 3, 4, 4, 5]
var newtechologylist = [];

$.each(techologylist , function (i, el) {
    
    if ($.inArray(el, newtechologylist ) === -1) {
        newtechologylist.push(el);
    } 
    
    else {
        var index = newtechologylist.indexOf(el);
        if(index > -1){
           newtechologylist.splice(index, 1);
        }
    }
});

document.write(newtechologylist);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
ozil
  • 6,930
  • 9
  • 33
  • 56