Divide Array into Equal Pairs

Easy
17
45.7% Acceptance

In this lab, you will be working on a function that takes an integer array nums consisting of 2 * n integers. Your task is to divide nums into n pairs such that:

  • Each element belongs to exactly one pair.
  • The elements present in a pair are equal.

The function should return true if nums can be divided into n pairs, otherwise return false.

Function Signature

/** * @param {number[]} nums * @return {boolean} */ export const divideArray = function(nums) { };

Example

divideArray([3,2,3,2,2,2]) // Output: true

Explanation:
There are 6 elements in nums, so they should be divided into 6 / 2 = 3 pairs.
If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy all the conditions.

divideArray([1,2,3,4]) // Output: false

Explanation:
There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy every condition.

Constraints

  • nums.length == 2 * n
  • 1 <= n <= 500
  • 1 <= nums[i] <= 500

Challenges

  1. Export divideArray function from index.js.
  2. Test your function with various examples and verify if it returns true or false based on the conditions provided.

Evaluation script demo

Here is the evaluation script for your challenges. Make sure to export the function as shown in the challenge description.

import fs from 'fs' import { divideArray } from '../index.js' // testlog is a log of test results const testlog = [] // this first block matches with - Challenge 1 try { const testResult = divideArray([3,2,3,2,2,2]) // perform some testing if(testResult === true) { testlog.push({ status: 'pass' }) } else { throw new Error('Function does not return correct output') } } catch(error) { testlog.push({ status: 'error', error: error.message || 'Challenge failed' }) } // this second block matches with - Challenge 2 try { const testResult = divideArray([1,2,3,4]) // perform some testing if(testResult === false) { testlog.push({ status: 'pass' }) } else { throw new Error('Function does not return correct output') } } catch(error) { testlog.push({ status: 'error', error: error.message || 'Challenge 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

Keep the "Setting up test environment script" exactly as provided in the partial lab material.

Initial file system for user

Keep the initial file system for user exactly as provided in the partial lab material.

Important Note: Always remember to never use any additional package.json files in the lab description. Everything should have been explained in the lab description above.