TESTEROPS

A pragmatic approach to QA and OPS

JavaScript Code Questions – Arrays

This contains js code questions regarding manipulations of array.

How do you find the missing number in a given integer array of 1 to 100

To find the missing number in an integer array of 1 to 100 using JavaScript with multiple arrays, you can follow these steps:

  1. Create two arrays:
    • The first array should contain all the integers from 1 to 100.
    • The second array should be the given integer array with one number missing.
  2. Use a loop to compare each element in the first array to the elements in the second array. When a number is found in the first array that is not in the second array, that number is the missing number.
// Given integer array with one missing number
const givenArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 98, 99, 100];

// Create an array with all integers from 1 to 100
const fullArray = Array.from(Array(100), (_, i) => i + 1);

// Loop through the full array and check if each number is in the given array
for (let i = 0; i < fullArray.length; i++) {
  if (!givenArray.includes(fullArray[i])) {
    console.log(`Missing number is: ${fullArray[i]}`);
    break;
  }
}

How do you find the duplicate number on a given integer array

Approach 1: Using a nested loop

This approach involves using a nested loop to compare each element in the array with every other element in the array. If two elements are found to be equal, one of them is a duplicate.

// Given integer array
const array = [1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10];

// Loop through the array and compare each element with every other element
for (let i = 0; i < array.length; i++) {
  for (let j = i + 1; j < array.length; j++) {
    if (array[i] === array[j]) {
      console.log(`Duplicate number found: ${array[i]}`);
      break;
    }
  }
}

Approach 2: Using a hash table

This approach involves creating a hash table to store the frequency of each element in the array. If an element is found to have a frequency greater than one, it is a duplicate.

// Given integer array
const array = [1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10];

// Create a hash table to store the frequency of each element
const frequency = {};
for (let i = 0; i < array.length; i++) {
  if (!frequency[array[i]]) {
    frequency[array[i]] = 1;
  } else {
    frequency[array[i]]++;
  }
}

// Loop through the hash table and check if any element has a frequency greater than one
for (const key in frequency) {
  if (frequency[key] > 1) {
    console.log(`Duplicate number found: ${key}`);
  }
}

Approach 3: Using the Set object

This approach involves using the Set object to keep track of unique elements in the array. If an element is already in the set, it is a duplicate.




// Given integer array
const array = [1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10];

// Create a set to store unique elements
const set = new Set();

// Loop through the array and check if each element is already in the set
for (let i = 0; i < array.length; i++) {
  if (set.has(array[i])) {
    console.log(`Duplicate number found: ${array[i]}`);
  } else {
    set.add(array[i]);
  }
}

There is an array with every element repeated twice except one. Find that element

To find the element that is not repeated twice in the given array, we can use the XOR (^) operator. The idea is to XOR all the elements in the array with each other. Since XOR of two same elements is 0, all the elements that are repeated twice will cancel each other out and we will be left with the element that is not repeated twice.

Here’s the code to find the non-repeated element in the array using the XOR operator:

// Given integer array
const array = [1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6];

let result = 0;

// XOR all the elements in the array with each other
for (let i = 0; i < array.length; i++) {
  result ^= array[i];
}

console.log(`Non-repeated element is: ${result}`);

How do you find the largest and smallest number in an unsorted integer array

This can be solved using different approaches

Approach 1 – Using the Math.max() and Math.min() methods:

const arr = [5, 3, 1, 7, 9];
const max = Math.max(...arr); // 9
const min = Math.min(...arr); // 1

Approach 2 – Using a for loop:

const arr = [5, 3, 1, 7, 9];
let max = arr[0];
let min = arr[0];
for (let i = 1; i < arr.length; i++) {
  if (arr[i] > max) {
    max = arr[i];
  }
  if (arr[i] < min) {
    min = arr[i];
  }
}
console.log(max); // 9
console.log(min); // 1

Approach 3 – Using the reduce() method:

const arr = [5, 3, 1, 7, 9];
const max = arr.reduce((a, b) => Math.max(a, b)); // 9
const min = arr.reduce((a, b) => Math.min(a, b)); // 1

Approach 4 – Using the sort() method

const arr = [5, 3, 1, 7, 9];
arr.sort((a, b) => a - b); // [1, 3, 5, 7, 9]
const min = arr[0]; // 1
const max = arr[arr.length - 1]; // 9

How do you find all pairs of an integer array whose sum is equal to a given number

We can use three different approaches for getting this

Approach 1 – Using nested for loops

function findPairs(arr, sum) {
  const pairs = [];

  for (let i = 0; i < arr.length; i++) {
    for (let j = i + 1; j < arr.length; j++) {
      if (arr[i] + arr[j] === sum) {
        pairs.push([arr[i], arr[j]]);
      }
    }
  }

  return pairs;
}

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const sum = 10;
const pairs = findPairs(arr, sum);

console.log(pairs); // [[1, 9], [2, 8], [3, 7], [4, 6]]

Approach 2 – Using a hash table

function findPairs(arr, sum) {
  const pairs = [];
  const hash = {};

  for (let i = 0; i < arr.length; i++) {
    const target = sum - arr[i];

    if (hash[target]) {
      pairs.push([target, arr[i]]);
    }

    hash[arr[i]] = true;
  }

  return pairs;
}

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const sum = 10;
const pairs = findPairs(arr, sum);

console.log(pairs); // [[1, 9], [2, 8], [3, 7], [4, 6]]

Approach 3 – Using the filter() method of arrays

function findPairs(arr, sum) {
  return arr
    .map((num, index) =>
      arr.slice(index + 1).filter((item) => item + num === sum).map((item) => [num, item])
    )
    .flat();
}

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const sum = 10;
const pairs = findPairs(arr, sum);

console.log(pairs); // [[1, 9], [2, 8], [3, 7], [4, 6]]