Javascript Array removeDuplicate()
// Remove Dupes from Google Reader v0.2 ///* w w w . jav a2 s . c om*/ // version 0.2 - 2008-05-28 // - moved Remove Dupes button next to the Refresh button // - dupes: mark as read, but no longer hides them (fix keyboard j,k bug) // - finds more dupes by splitting symbols (-, |) // - more helpful firebug console output (ie. # dupes removed) // // version 0.1 - 2008-02-09 // - first revision // // Last Updated 2008-05-28 // Copyright (c) 2008, Hao Chen // Released under the GPL license // http://www.gnu.org/copyleft/gpl.html // -------------------------------------------------------------------- // Contact: detect [at] hotmail [dot] com // -------------------------------------------------------------------- // // ==UserScript== // @name Remove Dupes from Google Reader // @description Because duplicate posts suck. // @include http://www.google.com/reader/* // ==/UserScript== Array.prototype.removeDuplicate = function() { // Here we remove duplicate values from the array of entries var array4 = new Array; var len = this.length; var entries = document.getElementById('entries'); for (var i = 0; i < len; i++) { iboth = this[i]; indexDash = iboth.lastIndexOf(' - '); if (indexDash != -1) { ititle = iboth.substr(0, indexDash); } else { ititle = iboth; } indexSlash = ititle.indexOf(' | '); if (indexSlash != -1) { ititle2 = ititle.substr(indexSlash + 3, ititle.length); } else { ititle2 = ititle; } var xx = true; for (var j = i + 1; j < len; j++) { jboth = this[j]; indexDash = jboth.lastIndexOf(' - '); if (indexDash != -1) { jtitle = jboth.substr(0, indexDash); } else { jtitle = jboth; } indexSlash = jtitle.indexOf(' | '); if (indexSlash != -1) { jtitle2 = jtitle.substr(indexSlash + 3, jtitle.length); } else { jtitle2 = jtitle; } if (iboth == jboth || ititle == jtitle || ititle2 == jtitle2) xx = false; } if (xx == true) { array4.push(iboth); } else { var entry = entries.childNodes[i]; entry.className = entry.className.replace('unread', '') + " read"; console.log("Dupe removed: " + ititle); } } console.log(len - array4.length, "dupes marked as read."); return array4; }
Array.prototype.removeDuplicate = function() { return this.reduce(function(last,current){ if(last.indexOf(current) === -1){ last.push(current);//w w w . j a va2s. c o m } return last; },[]) }; var array = [1,1,2,1,3,4,12,4,5,,12,3,4]; var a = array.concat([]); a = a.filter(function(item, pos) { return a.indexOf(item) == pos; }) console.log(a) console.log(array.removeDuplicate());
Array.prototype.removeDuplicate = function() { var result = []; var dict = {};/*ww w.ja v a 2 s . co m*/ for (var i = 0, len = this.length; i < len; ++i) { if (!dict[this[i]]) { result.push(this[i]); dict[this[i]] = 1; } } return result; }; var arr = [1, 1, 2, 3, 3, 4, 5, 6, 3, 2]; console.log(arr.removeDuplicate());
Array.prototype.removeDuplicate = function() { var obj = {};// w ww . j a va2 s . co m var arr = []; this.forEach(function(value, index) { if (!obj[value]) { arr.push(value); obj[value] = true; } }) return arr; }