js find duplicate elements in the array

for循环indexof()

var a=[1, 2, 2, 3, 4, 2, 2,5,36,4,78,3,3,3,3];

var b=[];

for (var i =0; i <a.length; i++) {
    
    
	if(b.indexOf(a[i])==-1 && a.indexOf(a[i])!==a.lastIndexOf(a[i])){
    
    
		b.push(a[i]);
	}
}
console.log(b);

Scan the code to get 1000+ front-end interview questions collection to try later

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42981560/article/details/109048258