-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsock_pair.js
More file actions
45 lines (40 loc) · 1.19 KB
/
sock_pair.js
File metadata and controls
45 lines (40 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"use strict";
let ar=[10, 10, 10, 20];
let n=2;
function sockMerchant(n, ar) {
let newPairs=[];
let sumOfPairs=[];
let totalPair
if(ar.length>=2){
ar.forEach(pair=>{
//create unique values
//to be used to iterate through the array provided
if(newPairs.indexOf(pair)==-1){
newPairs.push(pair);
}
});
}else{
return 0;
}
//using the newPairs, iterate through the array to find unique pairs
newPairs.forEach(val=>{
let filtered= ar.filter(thisVal=>thisVal==val);
if(filtered.length>1){
//lets see the number of identical values per filter
totalPair= filtered.length;
let eachPair;
//to see if the iterated values is a pair, divide by n(where n=2);
if(totalPair>=n){
eachPair= Math.floor(totalPair/n);
sumOfPairs.push(eachPair);
}
}
})
//lets sum the sumofPairs to get the total pairs now
if(sumOfPairs.length>0){
return sumOfPairs.reduce((a, b)=>a+b);
}else{
return 0;
}
}
console.log(sockMerchant(n, ar));