Dapatkan Total Pasangan dari Integer Array JavaScript

//[10, 20, 20, 10, 10, 30, 50, 10, 20]
//There is two pair of color 10 and one of color 20. There are three odd socks left. The number of pairs is 3.
function totalPair(n, ar) {
    let duplicateArray = [...ar]
    let duplicate = 0
    ar.forEach(col=>{
    let pairs = 0;
    duplicateArray.forEach(c=>{
        if(c === col){
        pairs++
        }
    })
    duplicateArray = duplicateArray.filter(a=>a!==col)
    duplicate +=  Math.floor(pairs / 2)
    })
    return duplicate
}
Husnain Syed