function uniq(a) {var seen = {};return a.filter(function(item) {return seen.hasOwnProperty(item) ? false : (seen[item] = true);});}
function uniq_fast(a) {var seen = {};var out = [];var len = a.length;var j = 0;for(var i = 0; i < len; i++) {var item = a[i];if(seen[item] !== 1) {seen[item] = 1;out[j++] = item;}}return out;}
/////
var r = [0,1,2,3,4,5,6,7,8,9],a = [],LEN = 1000,LOOPS = 1000;
while(LEN--)a = a.concat(r);
var d = new Date();for(var i = 0; i < LOOPS; i++)uniq(a);document.write('<br>uniq, ms/loop: ' + (new Date() - d)/LOOPS)
var d = new Date();for(var i = 0; i < LOOPS; i++)uniq_fast(a);document.write('<br>uniq_fast, ms/loop: ' + (new Date() - d)/LOOPS)
function* uniqIter(a) {let seen = new Set();
for (let x of a) {if (!seen.has(x)) {seen.add(x);yield x;}}}
// example:
function* randomsBelow(limit) {while (1)yield Math.floor(Math.random() * limit);}
// note that randomsBelow is endless
count = 20;limit = 30;
for (let r of uniqIter(randomsBelow(limit))) {console.log(r);if (--count === 0)break}
// exercise for the reader: what happens if we set `limit` less than `count` and why
function uniqueArray(array){if ($.isArray(array)){var dupes = {}; var len, i;for (i=0,len=array.length;i<len;i++){var test = array[i].toString();if (dupes[test]) { array.splice(i,1); len--; i--; } else { dupes[test] = true; }}}else {if (window.console) console.log('Not passing an array to uniqueArray, returning whatever you sent it - not filtered!');return(array);}return(array);}
var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];
var uniq = names.reduce(function(a,b){if (a.indexOf(b) < 0 ) a.push(b);return a;},[]);
console.log(uniq, names) // [ 'Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Carl' ]
// one linerreturn names.reduce(function(a,b){if(a.indexOf(b)<0)a.push(b);return a;},[]);
更快的uniq排序
可能有更快的方法,但这种方法相当不错。
var uniq = names.slice() // slice makes copy of array before sorting it.sort(function(a,b){return a > b;}).reduce(function(a,b){if (a.slice(-1)[0] !== b) a.push(b); // slice(-1)[0] means last item in array without removing it (like .pop())return a;},[]); // this empty array becomes the starting value for a
// one linerreturn names.slice().sort(function(a,b){return a > b}).reduce(function(a,b){if (a.slice(-1)[0] !== b) a.push(b);return a;},[]);
var toHash = [];var toList = [];
// add from ur data list to hash$(data.pointsToList).each(function(index, Element) {toHash[Element.nameTo]= Element.nameTo;});
// now convert hash to array// don't forget the "hasownproperty" else u will get random resultsfor (var key in toHash) {if (toHash.hasOwnProperty(key)) {toList.push(toHash[key]);}}
var dedupe = function(a){var hash={},ret=[];var hashFunction = function(v) { return ""+v; };var collect = function(h){if(hash.hasOwnProperty(hashFunction(h)) == false) // O(1){hash[hashFunction(h)]=1;ret.push(h); // should be O(1) for Arraysreturn;}};
for(var i=0; i<a.length; i++) // this is a loop: O(n)collect(a[i]);//OR: a.forEach(collect); // this is a loop: O(n)
return ret;}
var dedupe = function(a){var hash={};var isdupe = function(h){if(hash.hasOwnProperty(h) == false) // O(1){hash[h]=1;return true;}
return false;};
return a.filter(isdupe); // this is a loop: O(n)}
let a = [11,22,11,22];let b = []
b = [ ...new Set(a) ];// b = [11, 22]
b = Array.from( new Set(a))// b = [11, 22]
b = a.filter((val,i)=>{return a.indexOf(val)==i})// b = [11, 22]
function removeRepeats(ints) {var unique = {}var newInts = []
for (var i = 0; i < ints.length; i++) {var int = ints[i]
if (!unique[int]) {unique[int] = 1newInts.push(int)}}return newInts}
var example = [100, 100, 100, 100, 500]console.log(removeRepeats(example)) // prints [100, 500]