JSFiddle - React, Tailwind, and code Playground

by Adam Azad

HTML

<pre id="console">
</pre>

JavaScript

const arr = [1,4,5,6,3,6,6,3,2,];
let dups = [];

arr.forEach((a) => {
 
    let occurance = 0;
  	
    // use while loop to stop the loop when occurance reaches 2
	arr.forEach(b => {
		if (b == a) ++occurance
    });
	
	// add value to array if it occured more than once and not been recorded
    if (occurance > 1 && dups.indexOf(a) < 0){
		dups.push(a);
    }
   

});


document.querySelector('#console').innerHTML = JSON.stringify(dups);