AmitDiwan has Published 10744 Articles

How to remove every Nth element from an array JavaScript?

AmitDiwan

AmitDiwan

Updated on 26-Aug-2020 11:16:26

1K+ Views

Let’s say, we have to write a function remove Nth that takes in an array and a number n and it removes every nth element of the array in place.This can be done using the Array.prototype.splice() method and here is the code for doing so −Exampleconst arr = ['T', 'h', ... Read More

JavaScript Return the lowest index at which a value should be inserted into an array once it has been sorted (either in ascending or descending order).

AmitDiwan

AmitDiwan

Updated on 26-Aug-2020 11:13:33

189 Views

We have to write a function that returns the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted (either in ascending or descending order). The returned value should be a number.For example, Let’s say, we have a function ... Read More

Check if three consecutive elements in an array is identical in JavaScript

AmitDiwan

AmitDiwan

Updated on 26-Aug-2020 11:10:32

816 Views

We are required to write a JavaScript function, say checkThree() that takes in an array and returns true if anywhere in the array there exists three consecutive elements that are identical (i.e., have the same value) otherwise it returns false.Therefore, let’s write the code for this function −Exampleconst arr = ... Read More

What should be the correct Algorithm to Get Array B from Array A counting backwards from the last element in JavaScript?

AmitDiwan

AmitDiwan

Updated on 26-Aug-2020 11:08:10

93 Views

Consider the following binary array (Array A) −const arr = [1, 0, 1, 1, 1, 1, 0, 1, 1];When this array is passed through the function, say sumRight(), it produces the following output array (Array B) −const output = [1, 0, 4, 3, 2, 1, 0, 2, 1];Understanding the functionElements ... Read More

Filter an object based on an array JavaScript

AmitDiwan

AmitDiwan

Updated on 26-Aug-2020 11:05:15

776 Views

Let’s say. we have an array and an object like this −const arr = ['a', 'd', 'f']; const obj = {    "a": 5,    "b": 8,    "c": 4,    "d": 1,    "e": 9,    "f": 2,    "g": 7 };We are required to write a function that ... Read More

Sort array based on another array in JavaScript

AmitDiwan

AmitDiwan

Updated on 26-Aug-2020 11:03:57

3K+ Views

We are required to write a sorting function that sort an array based on the contents of another array.For example − We have to sort the original array such that the elements present in the below sortOrder array appear right at the start of original array and all other should ... Read More

Return the first duplicate number from an array in JavaScript

AmitDiwan

AmitDiwan

Updated on 26-Aug-2020 10:59:41

695 Views

We are required to write a function that returns the index of the very first element that appears at least twice in the array. If no element appears more than once, we have to return -1. The condition is that we have to do this in constant space (i.e., without ... Read More

Split one-dimensional array into two-dimensional array JavaScript

AmitDiwan

AmitDiwan

Updated on 26-Aug-2020 09:50:07

2K+ Views

We are required to write a function that takes in a one-dimensional array as the first argument and a number n as the second argument and we have to make n subarrays inside of the parent array (**if possible) and divide elements into them accordingly.** if the array contains 9 ... Read More

Check for Power of two in JavaScript

AmitDiwan

AmitDiwan

Updated on 26-Aug-2020 09:42:28

2K+ Views

We are required to write a function, say isPowerOfTwo() that takes in a positive number and returns a boolean based on the fact whether or not the number is some power of 2.For example −console.log(isPowerOfTwo(3)); //false console.log(isPowerOfTwo(32)); //true console.log(isPowerOfTwo(2048)); //true console.log(isPowerOfTwo(256)); //true console.log(isPowerOfTwo(22)); //falseLet’s write the code for this function, ... Read More

How do I write a function that takes an array of values and returns an object JavaScript?

AmitDiwan

AmitDiwan

Updated on 26-Aug-2020 09:41:04

562 Views

Let’s say, we are required to write a function classifyArray() that takes in an array which contains mixed data types and returns a Map() with the elements grouped by their data types.For example −// if the input array is: const arr = ['class', 2, [7, 8, 9], {"name": "Michael"}, Symbol('foo'), ... Read More

Advertisements