 # Divide Array into Equal Pairs

Easy
16
44.1% 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.

``````1import fs from 'fs'
2import { divideArray } 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	const testResult = divideArray([3,2,3,2,2,2])
10
11	// perform some testing
12	if(testResult === true) {
13		testlog.push({ status: 'pass' })
14	} else {
15		throw new Error('Function does not return correct output')
16	}
17} catch(error) {
18	testlog.push({
19		status: 'error',
20		error: error.message || 'Challenge failed'
21	})
22}
23
24// this second block matches with - Challenge 2
25try {
26	const testResult = divideArray([1,2,3,4])
27
28	// perform some testing
29	if(testResult === false) {
30		testlog.push({ status: 'pass' })
31	} else {
32		throw new Error('Function does not return correct output')
33	}
34} catch(error) {
35	testlog.push({
36		status: 'error',
37		error: error.message || 'Challenge failed'
38	})
39}
40
41// very important for the final length of `testlog` array to match the number of challenges, in this case - 2.
42
43// write the test log
44fs.writeFileSync('/home/damner/code/.labtests/testlog.json', JSON.stringify(testlog))
45
46// write the results array boolean. this will map to passed or failed challenges depending on the boolean value at the challenge index
47fs.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.