Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

get total pairs from 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
}
 
PREVIOUS NEXT
Tagged: #total #pairs #integer #array #javascript
ADD COMMENT
Topic
Name
4+5 =