Number of Good Pairs Lab
Easy
3
52.9% Acceptance
In this lab, you will be implementing a function called numIdenticalPairs
that counts the number of good pairs in a given array of integers. A pair (i, j)
is called good if nums[i] == nums[j]
and i
< j
.
The function should have the following signature:
/** * @param {number[]} nums * @return {number} */ function numIdenticalPairs(nums) { // Your code here }
Your implementation should meet the following requirements:
- It takes an array of integers
nums
as input. - The input array will have a length between 1 and 100.
- The elements in the input array will have values between 1 and 100.
- It returns the number of good pairs in the input array.
Example 1:
Input: nums = [1, 2, 3, 1, 1, 3] Output: 4 Explanation: There are four good pairs (0, 3), (0, 4), (3, 4), (2, 5) 0-indexed.
Example 2:
Input: nums = [1, 1, 1, 1] Output: 6 Explanation: Each pair in the array are good.
Example 3:
Input: nums = [1, 2, 3] Output: 0
Make sure to export the function so that it can be used by the evaluation script:
export { numIdenticalPairs };
Challenges
- Implement the
numIdenticalPairs
function. - Export the
numIdenticalPairs
function.
Evaluation script
1import fs from 'fs' 2import { numIdenticalPairs } from '../index.js' 3 4// testlog is a log of test results 5const testlog = [] 6 7// this first block matches with - Challenge 1 8try { 9 // Test cases: 10 let testCase1 = [1, 2, 3, 1, 1, 3]; 11 let testCase2 = [1, 1, 1, 1]; 12 let testCase3 = [1, 2, 3]; 13 14 // Expected results: 15 let expectedResult1 = 4; 16 let expectedResult2 = 6; 17 let expectedResult3 = 0; 18 19 // Test the numIdenticalPairs function 20 if ( 21 numIdenticalPairs(testCase1) === expectedResult1 && 22 numIdenticalPairs(testCase2) === expectedResult2 && 23 numIdenticalPairs(testCase3) === expectedResult3 24 ) { 25 testlog.push({ status: 'pass' }); 26 } else { 27 throw new Error('Incorrect results for numIdenticalPairs function'); 28 } 29 } catch (error) { 30 testlog.push({ 31 status: 'error', 32 error: error.message || 'Challenge 1 failed' 33 }) 34 } 35 36 // this second block matches with - Challenge 2 37 try { 38 if (numIdenticalPairs !== undefined) { 39 testlog.push({ status: 'pass' }); 40 } else { 41 throw new Error('numIdenticalPairs is not exported'); 42 } 43 } catch (error) { 44 testlog.push({ 45 status: 'error', 46 error: error.message || 'Challenge 2 failed' 47 }) 48 } 49 50// very important for the final length of `testlog` array to match the number of challenges, in this case - 2. 51 52// write the test log 53fs.writeFileSync('/home/damner/code/.labtests/testlog.json', JSON.stringify(testlog)) 54 55// write the results array boolean. this will map to passed or failed challenges depending on the boolean value at the challenge index 56fs.writeFileSync(process.env.UNIT_TEST_OUTPUT_FILE, JSON.stringify(testlog.map(result => result.status === 'pass')))
Setting up test environment script
1#!/bin/bash 2set -e 1 3 4mkdir -p /home/damner/code/.labtests 5 6cat > /home/damner/code/.labtests/package.json << EOF 7{ 8 "type": "module" 9} 10EOF 11 12cd /home/damner/code/.labtests 13mv $TEST_FILE_NAME ./nodecheck.test.js 14 15# import puppeteer doesn't work without it 16npm link puppeteer 17 18node nodecheck.test.js 2>&1 | tee evaluationscript.log
Initial file system for user
/** * @param {number[]} nums * @return {number} */ function numIdenticalPairs(nums) { // Your code here } // Make sure to export numIdenticalPairs function export { numIdenticalPairs };
{ "name": "codedamn-lab", "type": "module" }
tabs: ['index.js']
terminals: ['yarn install']