<!DOCTYPE html>
<html lang="en">
<head>
<style>
div{
display: inline-block;
width: 30px;
height: 30px;
border: 1px solid red;
}
</style>
</head>
<body>
<div class="element">-</div>
<div class="element">-</div>
<div class="element">-</div>
<div class="element">-</div>
<div class="element">-</div>
<div class="element">-</div>
<div class="element">-</div>
<div class="element">-</div>
<script>
var elm = document.querySelectorAll('.element');
elm.forEach(el=> el.onclick=e=>{
el.innerHTML = Math.floor(Math.random() * 9);
col();
});
function col(){
elm.forEach(el=>el.style.backgroundColor = 'white');
var arr = [].slice.call(elm);
arr.filter((el, i)=> {
while (i--) if (+el.textContent == +arr[i].textContent){
el.style.backgroundColor = 'red';
arr[i].style.backgroundColor = 'red';
}
});
};
</script>
</script>
</body>
</html>
может можно как то проще