How to count possible pair elements in an array in JavaScript

There is no better way to be a better programmer than by learning algorithms and solving problems. This article contains solution to a challenge I came across on hackerrank.

Given an array of numbers num[] of N integers (the number of elements in the array), we are to count the number of possible pairs of element in the given array. Example:

const num = [2,3,4,2,1,4,6,7,3]
// output = 3
// (num[0],num[3]),(num[1],num[8]) and (num[2],num[5]) are the only possible pairs

const num = [5,3,5,0,1,8]
// output = 1
// (num[0],num[2]) is the only possible pair

Lets take a sample

const num = [23,20,89,20,10,9,14,14,10,34];

const pairElements = (arr, n) => {
  // code goes in here
}

pairElements(num, 10)

We first create a variable num and assign an array containing 10 integers (elements). Then we declare a pairElements() function, which contains two parameters (arr and n). Finally, we call the function, passing num and 10(number of elements) as arr and n parameters respectively.

Now, within our pairElement() function scope, we write the following code:

let count = 0; // store count

arr.sort() // sort the array

From the above, we set our count variable to be equal to 0. We then sort our array so that equal elements can be next to each other.

Sort() method converts the elements into string and return the elements in ascending order based on the UTF-16 code units values

If we are to log the above code to the console, we'll have [10, 10, 14, 14, 20, 20, 23, 34, 89, 9].


for (let i = 0; i < n-1;){

  // if the first two elements make a pair
  if(arr[i] == arr[i + 1]){
    count++ // increment by 1

    i = i + 2  // skip the current pair
  }
  // if current element doesn't pair with the next element
  else{
  i++
  }
}

return count

We proceed to iterate our array using a for loop. We first off set our index variable i to 0. Since array in JavaScript is zero-based index, we subtract one from n and declare a condition; if i is less than n-1 (length of the array). Within the for loop scope, we declare a condition statement (if...else).

If the current element is equal to the next element, we increase the count value by 1 and skip the pair elements by increasing the index variable by 2. Else, if the current element is not equal to the next, the index variable is increased by 1. Finally we return the value of count.

Putting everything together;

const num = [23,20,89,20,10,9,14,14,10,34]

const pairElements = (arr, n) => {
  let count = 0; // store count

  arr.sort() // sort the array

  for (let i = 0; i < n-1;){

    // if the first two elements make a pair
    if(arr[i] == arr[i + 1]){
    count++ // increment by 1

    i = i + 2  // skip the current pair
    }
    // if current element doesn't pair with the next element
    else{
    i++
    }
}

return count;
}

pairElements(num, 10)

When you log it to the console, it will print:

3

So we have 3 pairs of elements in our array.

That's a wrap!

So that's how we count possible pairs in an array. If you want to learn and solve more algorithms, visit hackerrank. If you enjoy reading this article, kindly leave a like👍 or comment💬

Thanks for reading👋❤️