Data Structure in JavaScript Array

findDisappearedNumbers

var findDisappearedNumbers = function (nums) {
  for (let i = 0; i < nums.length; i++) {
    let q = Math.abs(nums[i]) - 1;
    nums[q] = Math.abs(nums[q]) * -1;
  }

  let results = [];

  for (let i = 0; i < nums.length; i++) {
    if (nums[i] > 0) {
      results.push(i + 1);
    }
  }

  return results;
};

const res = findDisappearedNumbers([1, 2, 3, 7, 4, 6, 9]);
console.log(findDisappearedNumbers([1, 2, 3, 7, 4, 6, 9]));
console.log(res);

Problem. Write a program to find the sum of largest and smallest numbers in the without sorting an integer array

INPUT: [10, 22, 4, 5, 2, 6, 8, 8,10, 10, 10, 2]

OUTPUT: 24
var largest = [];

var smallest = Infinity;

var arr = [10, 22, 4, 5, 2, 6, 8, 8, 10, 10, 10, 2];

for (var i = 0; i < arr.length; i++) {
  if (arr[i] > largest) {
    largest = arr[i];
  }

  if (arr[i] < smallest) {
    smallest = arr[i];
  }
}

console.log(largest + smallest);

findMissingNumbers

function findMissingNumbers(arr) {
  var missingNumbers = [];

  for (var i = arr[0]; i <= arr[arr.length - 1]; i++) {
    if (arr.indexOf(i) === -1) {
      missingNumbers.push(i);
    }
  }

  if (missingNumbers.length == 1) {
    return missingNumbers[0];
  } else if (missingNumbers.length > 1) {
    return missingNumbers;
  } else {
    return null;
  }
}

const result = findMissingNumbers([1, 2, 3, 4, 6, 7, 8, 10]);
const result2 = findMissingNumbers([55, 56, 57, 59]);
const result3 = findMissingNumbers([23, 24, 25, 26]);

console.log(result);
console.log(result2);
console.log(result3);

How do you declare and initialize an array in JavaScript?

first method

const myArray = new Array();

myArray.push(1, 2, 3);
myArray.push("Hello world!");

console.log(myArray);

// second method

const myArray2 = [];

myArray2.push(1, 2);
console.log(myArray2);

Question => What is the difference between push() and pop() methods for arrays?

Ans => The push() and pop() methods are used to add and remove elements from the end of an array in JavaScript. The main difference between the two methods is that push() adds an element to the end of the array, while pop() removes the last element from the array.

example of how to use the push() method:

const myArray = [];

myArray.push(1);
myArray.push(2);
myArray.push(3);

console.log("This using push method in the Array", myArray); // [1, 2, 3]

//  example of how to use the pop() method:
const lastElement = myArray.pop();

console.log("This using Pop method in the Array", lastElement); // 3
console.log("After push and pop mthod ", myArray); // [1, 2]

Question => How do you remove an element from an array in JavaScript?

1 The pop() method: The pop() method removes the last element from the array and returns it.

const myArray = [1, 2, 3];

const lastElement = myArray.pop();

console.log("Removed Element with help of Push() Method", lastElement); // 3
console.log(myArray); // [1, 2]

// 2 The shift() method: The shift() method removes the first element from the array and returns it.

const myArray2 = [1, 2, 3];

ReverseArray

// Question => Implement a function to reverse an array in-place.

function reverseArray(array) {
  let start = 0;
  let end = array.length - 1;

  while (start < end) {
    const temp = array[start];
    array[start] = array[end];
    array[end] = temp;
    start++;
    end--;
  }
}

const array = [1, 2, 3, 4, 5];

reverseArray(array);

console.log(array); // [5, 4, 3, 2, 1]

Question => Explain the difference between sparse and dense arrays.

const sparseArray = {
  // The keys of the object represent the indices of the non-zero elements.
  // The values of the object represent the values of the non-zero elements.
  0: 1,
  5: 2,
  10: 3,
};

for (const [index, value] of Object.entries(sparseArray)) {
  console.log(`The value at index ${index} is ${value}`);
}

question => Explain the time complexity of accessing an element in an array.

ans => The time complexity of accessing an element in an array is O(1), also known as constant time. This means that the time it takes to access an element in an array does not depend on the size of the array.

For example, if an array has 10 elements, then the address of the element at index 0 is base_address + 0 * element_size, the address of the element at index 1 is base_address + 1 * element_size, and so on.

When we access an element in an array, the computer simply calculates the address of the element and then reads the data from that address. This process takes a constant amount of time, regardless of the size of the array.

There are a few things that can affect the time it takes to access an element in an array. For example, if the array is not stored in contiguous memory, then the computer may need to perform a cache miss, which can add some overhead. However, in general, the time complexity of accessing an element in an array is O(1).

const myArray = [1, 2, 3, 4, 5];

const element = myArray[3];

console.log(element);

// In this example, we are creating an array called myArray with five elements. We are then accessing the element at index 2, which is the number 3.

// The time complexity of this code is O(1), because the time it takes to access the element does not depend on the size of the array. In this case, the array has 5 elements, but the code would still take the same amount of time to execute if the array had 100 elements or 1000 elements.

Read More Topics

System Design Syllabus

Introduction to System Design

Architectural Patterns in System Design

Scalability and Performance in System Design

Database Design in System Design

Distributed Systems in System Design

System Integration and APIs in System Design

Cloud Computing in Sestem Design

Containerization and Orchestration in System Design

High Availability and Disaster Recovery

Security in System Design

Performance Tuning and Optimization

Case Studies and Real-World Projects

B.Tech 4 YEAR CS/IT PROJECTS And Placement

Face Detection Project Idea

Weather Forecasting APP in MERN Project

JavaScript Tips and Trick

Leave a Comment

Skip to content