Sometimes you want to remove duplicates in a JavaScript array, like this:
var b=new Array(1, 1, 2, 3, 4);
alert(unique(b));
Use the following functions to achieve this.
/**
* Removes duplicates in the array 'a'
* @author Johan Känngård, http://dev.kanngard.net
*/
function unique(a) {
tmp = new Array(0);
for(i=0;i<a.length;i++){
if(!contains(tmp, a[i])){
tmp.length+=1;
tmp[tmp.length-1]=a[i];
}
}
return tmp;
}
/**
* Returns true if 's' is contained in the array 'a'
* @author Johan Känngård, http://dev.kanngard.net
*/
function contains(a, e) {
for(j=0;j<a.length;j++)if(a[j]==e)return true;
return false;
}