Page 1 of 1

Complete the sockMerchant function using JavaScript

Posted: Mon May 02, 2022 11:38 am
by answerhappygod
Complete the sockMerchant function using
JavaScript
Complete The Sockmerchant Function Using Javascript 1
Complete The Sockmerchant Function Using Javascript 1 (59.5 KiB) Viewed 37 times
Complete The Sockmerchant Function Using Javascript 2
Complete The Sockmerchant Function Using Javascript 2 (15.27 KiB) Viewed 37 times
Complete The Sockmerchant Function Using Javascript 3
Complete The Sockmerchant Function Using Javascript 3 (36.05 KiB) Viewed 37 times
Complete The Sockmerchant Function Using Javascript 4
Complete The Sockmerchant Function Using Javascript 4 (69.53 KiB) Viewed 37 times
There is a large pile of socks that must be paired by color. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. Example n=7 ar = (1,2,1,2,1,3,2] There is one pair of color 1 and one of color 2. There are three odd socks left, one of each color. The number of pairs is 2. Function Description Complete the sockMerchant function in the editor below. sockMerchant has the following parameter(s): • int n: the number of socks in the pile • int ar[n]: the colors of each sock Returns • int: the number of pairs Input Format The first line contains an integer n, the number of socks represented in ar. The second line contains n space-separated integers, ar, the colors of the socks in the pile. Constraints .1<n< 100 .1 < ar < 100 where 0 <i<n
Sample Output 3 Explanation 50 20 20 10 10 20 10 10 30 There are three pairs of socks.
1 "use strict'; 2 3 const fs = require('fs'); 4. 5 process. stdin.resume(); 6 process.stdin.setEncoding('utf-8'); 7 8 let inputString = ''; 9 let currentLine = 0; 10 11 process.stdin.on('data', function(inputstdin) { 12 inputString += inputStdin; 13 }); 14 15 process.stdin.on('end', function() { 16 inputString = inputString.split('\n'); 17 18 main(); 19}); 20
* * * 如引见8叫的阳卻%的如4&B叶归归口89mm 21 function readLine() { 22 return inputString[currentLine++]; 23 } 24 25 /* 26 * Complete the 'sockMerchant' function below. 27 28 * The function is expected to return an INTEGER. 29 * The function accepts following parameters: 30 1. INTEGER n 31 2. INTEGER_ARRAY ar 32 */ 33 34 function sockMerchant(n, ar) { 35 // Write your code here 36 37 } 38 39 function main() { 40 const ws = fs.createWriteStream(process.env.OUTPUT_PATH); 41 const n = parseInt(readLine().trim(), 10); 43 44 const ar = readLine().replace(/\s+$/8, '').split(' ').map(arTemp => parseInt(arTemp, 10)); 45 46 const result = sockMerchant(n, ar); 47 48 ws.write(result + '\n'); 49 50 ws.end(); 51 } 42