Javascript Articles - Page 201 of 534

Find first duplicate item in array in linear time JavaScript

AmitDiwan
Updated on 25-Nov-2020 04:51:22

332 Views

We are required to write a JavaScript function that takes in a read only array of n + 1 integers between 1 and n.The function should find one number that repeats in linear time and using at most O(n) space.For example If the input array is −const arr = [3 4 1 4 1];Then the output should be −const output = 1;If there are multiple possible answers ( like above ), we should output any one. If there is no duplicate, we should output -1.Exampleconst arr = [3, 4, 1, 4, 1]; const findRepeatedNumber = (arr = []) => { ... Read More

JavaScript Quicksort Recursive

Alshifa Hasnain
Updated on 17-Jan-2025 19:47:24

1K+ Views

In this article, we will learn to implement Quicksort recursively in JavaScript. Quicksort is one of the most efficient and widely used sorting algorithms, known for its divide-and-conquer approach. What is Quicksort? Quicksort is a divide-and-conquer sorting algorithm that sorts an array by selecting a pivot element and partitioning the array into two subarrays: elements smaller than the pivot and elements greater than or equal to the pivot. It then recursively sorts the subarrays.  Algorithm Quicksort follows the below steps − Step 1 − Make any element the pivot (preferably first or last, ... Read More

JavaScript - summing numbers from strings nested in array

AmitDiwan
Updated on 24-Nov-2020 11:06:51

208 Views

Suppose, we have an array that contains some demo credit card numbers like this −const arr = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'];We have been tasked with creating a function that takes in this array. The function must return the credit card number with the greatest sum of digits.If two credit card numbers have the same sum, then the last credit card number should be returned by the function.ExampleThe code for this will be −const arr = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260']; const findGreatestNumber = (arr) => {    let n, i = 0, sums;    sums = [];    while (i < ... Read More

Enter a number and write a function that adds the digits together on button click in JavaScript

AmitDiwan
Updated on 24-Nov-2020 11:04:53

646 Views

We are required to write a JavaScript program that provides users an input to fill in a number.And upon filling when the user clicks the button, we should display the sum of all the digits of the number.ExampleThe code for this will be −JavaScript Code −function myFunc() {    var num = document.getElementById('digits').value;    var tot = 0;    num.split('').forEach( function (x) {       tot = tot + parseInt(x,10);    }); document.getElementById('output').innerHTML = tot; }HTML Code − Submit OutputAnd the output will be &miuns;After clicking “Submit” −

Alphanumeric sorting using JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:58:22

439 Views

We have a mixed array that we need to sort by alphabet and then by digit −const arr = ['Ab-1', 'Ab-11', 'Ab-12', 'ab-10', 'ab-100', 'ab-101', 'ab2', 'ab-3', 'ab-105'];ExampleThe code for this will be −const arr = ['Ab-1', 'Ab-11', 'Ab-12', 'ab-10', 'ab-100', 'ab-101', 'ab2', 'ab-3', 'ab-105']; const alphaNumericSort = (arr = []) => {    arr.sort((a, b) => {       const aPart = a.split('-');       const bPart = b.split('-');       return aPart[0].toLowerCase().localeCompare(bPart[0].toLowerCase()) || aPart[1] - bPart[1];    }); }; alphaNumericSort(arr); console.log(arr);OutputAnd the output in the console will be −[    'Ab-1', 'ab-2',    'ab-3', 'ab-10', ... Read More

Turning a 2D array into a sparse array of arrays in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:57:14

248 Views

Suppose, we have a 2-D array like this −const arr = [    [3, 1],    [2, 12],    [3, 3] ];We are required to write a JavaScript function that takes in one such array.The function should then create a new 2-D array that contains all elements initialized to undefined other than the element's index present in the input array.Therefore, for the input array, output[3][1] = 1; output[2][12] = 1; output[3][3] = 1;And rest all elements should be initialized to undefinedTherefore, the final output should look like −const output = [    undefined,    undefined,    [       ... Read More

Sort by index of an array in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:55:30

3K+ Views

Suppose we have the following array of objects −const arr = [    {       'name' : 'd',       'index' : 3    },    {       'name' : 'c',       'index' : 2    },    {       'name' : 'a',       'index' : 0    },    {       'name' : 'b',       'index' : 1    } ];We are required to write a JavaScript function that takes in one such array.The function should sort this array in increasing order according to ... Read More

Sorting an array by price in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:52:50

2K+ Views

Suppose we have an array of objects that contains data about some houses and price like this −const arr = [    {       "h_id": "3",       "city": "Dallas",       "state": "TX",       "zip": "75201",       "price": "162500"    },    {       "h_id": "4",       "city": "Bevery Hills",       "state": "CA",       "zip": "90210",       "price": "319250"    },    {       "h_id": "5",       "city": "New York",       "state": "NY",     ... Read More

Returning the highest value from an array in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:48:01

299 Views

We are required to write a JavaScript function that takes in an array of Numbers. Our function should iterate through the array and pick the greatest (largest) element from the array and return that element.ExampleThe code for this will be −const arr = [5, 3, 20, 15, 7]; const findGreatest = (arr = []) => {    let greatest = -Infinity;    if(!arr?.length){       return null;    };    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(el < greatest){          continue;       };       greatest = el;    };    return greatest; }; console.log(findGreatest(arr));OutputAnd the output in the console will be −20

Get the smallest array from an array of arrays in JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:46:46

532 Views

Suppose, we have a nested array of arrays like this −const arr = [    ["LEFT", "RIGHT", "RIGHT", "BOTTOM", "TOP"],    ["RIGHT", "LEFT", "TOP"],    ["TOP", "LEFT"] ];We are required to write a JavaScript function that takes in one such array. The function then should pick the smallest subarray (smallest in sense of a number of elements contained) and return it.ExampleThe code for this will be −const arr = [    ["LEFT", "RIGHT", "RIGHT", "BOTTOM", "TOP"],    ["RIGHT", "LEFT", "TOP"],    ["TOP", "LEFT"] ]; const findShortest = (arr = []) => {    const res = arr.reduce((acc, val, ind) => ... Read More

Advertisements