Greatest Element in a Multi-Dimensional Array in JavaScript

AmitDiwan
Updated on 20-Oct-2020 12:04:11

141 Views

We have to write a simple function in JavaScript that takes in an array of Numbers (nested to any level) and return the greatest number present in the array.For example:If the input array is −const arr = [    34, 65, 67,    [       43, 76, 87, 23, 56, 7,       [          54, 7, 87, 23, 79, 994, 2       ],       54    ], 54, 4, 2 ];Then the output should be −994We will use recursion to find the greatest number in the array, Therefore, let’s ... Read More

Cumulative Average of Pair of Elements in JavaScript

AmitDiwan
Updated on 20-Oct-2020 12:00:52

228 Views

We have an array of numbers and are required to write a function that returns an array with the average of the corresponding element and its predecessor.For the first element, as there are no predecessors, so that very element should be returned.Let’s write the code for this function, we will use the Array.prototype.map() function to solve this problem.ExampleThe code for this will be −const arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, 4, 2, 1]; const consecutiveAverage = arr => {    return arr.map((el, ind, array) => {       const first = (array[ind-1] || ... Read More

Insert Empty String in Place of Repeating Values in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:58:16

449 Views

We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end.For example: If we find 4 duplicate values we have to remove then all and insert four empty strings at the end.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 5, 12, 1, 23, 4, 1]; const deleteAndInsert = arr => {    const creds = arr.reduce((acc, val, ind, array) => {       ... Read More

Detect Largest Element in Nested Array of Numbers in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:55:28

204 Views

We have to write a simple function in JavaScript that takes in an array of Numbers (nested to any level) and return the greatest number present in the array.For example: If the input array is −const arr = [    34, 65, 67,    [       43, 76, 87, 23, 56, 7,       [          54, 7, 87, 23, 79, 994, 2       ],       54    ], 54, 4, 2 ];Then the output should be −994We will use recursion to find the greatest number in the array.ExampleThe code ... Read More

Detect First Non-Unique Element in Array in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:53:28

169 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. We have to do this in constant space (i.e., without utilizing extra memory).So, let's write the solution for this problem.We will use a for loop to iterate over the array and use the Array.prototype.lastIndexOf() method to check for duplicacy.ExampleThe code for this will be −const arr1 = [0, 1, 1, 2, 3, 4, 4, 5]; const firstRedundant = arr => {    for(let i ... Read More

Remove Consecutive Duplicate Entries from an Array in JavaScript Using Recursion

AmitDiwan
Updated on 20-Oct-2020 11:51:32

507 Views

We are supposed to write a function that takes in an array of number/string literals. The function should remove all the redundant consecutive elements of the array without using extra memory space.For example, if the input array is −const arr = [17, 17, 17, 12, 12, 354, 354, 1, 1, 1];Then the output should be −const output = [17, 12, 354, 1];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [17, 17, 17, 12, 12, 354, 354, 1, 1, 1]; const comp = (arr, len = 0, deletable = false) => { ... Read More

Combine Identical Entries Together in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:49:38

97 Views

We have an array of numbers that have got identical entries. We are required to write a function that takes in the array and groups all the identical entries into one subarray and returns the new array thus formed.For example: If the input array is −const arr = [234, 65, 65, 2, 2, 234];// then the output should be −const output = [[234, 234], [65, 65], [2, 2]];We will use a Hashmap to keep a track of the elements already occurred and iterate over the array using a for loop.Therefore, let’s write the code for this function −ExampleThe code for ... Read More

Array Flattening Using Loops and Recursion in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:48:03

628 Views

We are required to write a JavaScript array function that takes in a nested array with false values as well and returns an array with all the elements present in the array without any nesting.For example: If the input is −const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], [6]];Then the output should be −const output = [1, 2, 3, 4, 5, false, 6, 5, 8, null, 6];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], ... Read More

Remove All Instances of Repeated Elements from Array in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:46:45

102 Views

We are required to write a function that takes in an array and returns a new array that have all duplicate values removed from it. The values that appeared more than once in the original array should not even appear for once in the new array.For example, if the input is −const arr = [763, 55, 43, 22, 32, 43, 763, 43];The output should be −const output = [55, 22, 32];Array.prototype.indexOf(): It returns the index of first occurrence of searched string if it exists, otherwise -1.Array.prototype.lastIndexOf(): It returns the index of last occurrence of searched string if it exists, otherwise ... Read More

Keep Only Alphanumerals in a JavaScript String

AmitDiwan
Updated on 20-Oct-2020 11:43:21

128 Views

We are required to write a JavaScript function that takes in a string that might contain some special characters.The function should return a new string should have all special characters replaced with their corresponding ASCII value.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'Th!s !s @ str!ng th@t cont@!ns some special characters!!'; const specialToASCII = str => {    let res = '';    for(let i = 0; i < str.length; i++){       if(+str[i] || str[i].toLowerCase() !== str[i].toUpperCase() || str[i] === ' '){          res += ... Read More

Advertisements