Search
 
SCRIPT & CODE EXAMPLE
 

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
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: toast info 
Javascript :: react datepicker 
Javascript :: javascript single thread 
Javascript :: node js add new object to object array json 
Javascript :: mean stack 
Javascript :: multi-dimensional array js 
Javascript :: copy object with new property in js 
Javascript :: jquery ajax methods 
Javascript :: node fs existssync 
Javascript :: math.max 
Javascript :: export function node js 
Javascript :: react-query dependent query 
Javascript :: what is javascript runtime 
Javascript :: passing argument to function handler functional compoent javascript react 
Javascript :: how to make an array of a value from 1 to the number 
Javascript :: jQuery intellisence in VSCode 
Javascript :: position of the mouse cursor javascript 
Javascript :: counting sheep 
Javascript :: trim a string in javascript 
Javascript :: jquery onclick multiple buttons 
Javascript :: alpine js 
Javascript :: 1 day ago javascript 
Javascript :: ckeditor inline editor example 
Javascript :: javascript count number of clicks limit 
Javascript :: set time out 
Javascript :: javascript replace tag 
Javascript :: else return 
Javascript :: js for loop plus number 
Javascript :: react: fow to use find(to get the id of a element 
Javascript :: if () { } 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =