Javascript: Duplikate entfernen / remove duplicates

less than 1 minute read

Javascript: Duplikate entfernen / remove duplicates thumb image

Dieses Script entfernt Duplikate sowohl aus Strings, als auch aus Arrays.

Sie ist einfach und schnell.

Getestet unter IE6/7, Firefox 2, Opera 9.02, Netscape 7

function removeDuplicates(hasDup){
	
	var noDup = new hasDup.constructor;
	
	if(noDup.push) {
		noDup[0] = hasDup[0];
	
		for(i=1;i<hasDup.length;i++){
			for(j=0;j<noDup.length;j++)
				if(hasDup[i] == noDup[j])
					break;
			if(j<noDup.length) continue;
			
			noDup.push(hasDup[i]);
		}
	} else {
		noDup = hasDup.charAt(0);
			
		for(i=1;i<hasDup.length;i++){
			for(j=0;j<noDup.length;j++)
				if(hasDup.charAt(i) == noDup.charAt(j))
					break;
			if(j<noDup.length) continue;
	
			noDup = noDup.concat(hasDup.charAt(i));
		}
	}
	
	return noDup;
}

Download

Download removeDuplicates.js

Comments

Leave a comment — Edit this page on github and issue a PR for your comment please. HowTo?