Remove Repeat Item

function removeRepeatItem1(type){ // create a empty array var arr=[],arr_index=1; arr[0]=type[0]; for(var i=0 ;i<type.length;i++){ var flag=true; for(var j=0; j<arr.length;j++){ if(type[i]==arr[j]){ flag=false; break; } } if(flag){ arr[arr_index]=type[i]; arr_index++; } } return arr; // return a Array } function removeRepeatItem2(type){ var ary = type,json = {},str2 = ""; for (var i = 0; i < ary.length; i++) { json["a"+ary[i]] = ary[i]; } for (var key in json) { str2 += json[key]+','; } return str2; // return a String } //how to use var b = [1,23,24,1,1,23,4,6]; var a = ["asd","45","asd","jaksdjksd","ew","ew","i"]; console.log(removeRepeatItem1(a)); // => ["asd", "45", "jaksdjksd", "ew", "i"] console.log(removeRepeatItem2(b)); // => 1,23,24,4,6, //new add var arr = [1,1,1,2,3,42,4,4,4,5]; var cur,prev; var newArr = arr.filter(function(a,index){ cur = a; prev = arr[index + 1]; return cur != prev; }); console.log(newArr);

3 Responses

Hi ! I have a suggestion for a more minimized code :) hope you can check this out :)
var arr = [1,1,1,2,3,42,4,4,4,5];
var cur,prev;
var newArr = arr.filter(function(a,index){
cur = a;
prev = arr[index + 1];
return cur != prev;
});
console.log(newArr);
@Kim Maravilla That's great ! Thank you~
You're welcome :)

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.