Number of Good Pairs Lab

Easy
7
62.1% 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 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

1. Implement the `numIdenticalPairs` function.
2. Export the `numIdenticalPairs` function.

Evaluation script

``````import fs from 'fs'
import { numIdenticalPairs } from '../index.js'

// testlog is a log of test results
const testlog = []

// this first block matches with - Challenge 1
try {
// Test cases:
let testCase1 = [1, 2, 3, 1, 1, 3];
let testCase2 = [1, 1, 1, 1];
let testCase3 = [1, 2, 3];

// Expected results:
let expectedResult1 = 4;
let expectedResult2 = 6;
let expectedResult3 = 0;

// Test the numIdenticalPairs function
if (
numIdenticalPairs(testCase1) === expectedResult1 &&
numIdenticalPairs(testCase2) === expectedResult2 &&
numIdenticalPairs(testCase3) === expectedResult3
) {
testlog.push({ status: 'pass' });
} else {
throw new Error('Incorrect results for numIdenticalPairs function');
}
} catch (error) {
testlog.push({
status: 'error',
error: error.message || 'Challenge 1 failed'
})
}

// this second block matches with - Challenge 2
try {
if (numIdenticalPairs !== undefined) {
testlog.push({ status: 'pass' });
} else {
throw new Error('numIdenticalPairs is not exported');
}
} catch (error) {
testlog.push({
status: 'error',
error: error.message || 'Challenge 2 failed'
})
}

// very important for the final length of `testlog` array to match the number of challenges, in this case - 2.

// write the test log
fs.writeFileSync('/home/damner/code/.labtests/testlog.json', JSON.stringify(testlog))

// write the results array boolean. this will map to passed or failed challenges depending on the boolean value at the challenge index
fs.writeFileSync(process.env.UNIT_TEST_OUTPUT_FILE, JSON.stringify(testlog.map(result => result.status === 'pass')))``````

Setting up test environment script

``````#!/bin/bash
set -e 1

mkdir -p /home/damner/code/.labtests

cat > /home/damner/code/.labtests/package.json << EOF
{
"type": "module"
}
EOF

cd /home/damner/code/.labtests
mv \$TEST_FILE_NAME ./nodecheck.test.js

# import puppeteer doesn't work without it

node nodecheck.test.js 2>&1 | tee evaluationscript.log``````

Initial file system for user

``````/**
* @param {number[]} nums
* @return {number}
*/
function numIdenticalPairs(nums) {
``````{
``````tabs: ['index.js']