AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 372 of 840

Using recursion to remove consecutive duplicate entries from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 557 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 − How the Algorithm Works The recursive function works by checking each element against its ...

Read More

Filter JavaScript array of objects with another array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 642 Views

Suppose, we have an array of objects like this − const arr = [ {area: 'NY', name: 'Bla', ads: true}, {area: 'DF', name: 'SFS', ads: false}, {area: 'TT', name: 'SDSD', ads: true}, {area: 'SD', name: 'Engine', ads: false}, {area: 'NSK', name: 'Toyota', ads: false}, ]; We are required to write a JavaScript function that takes in one such array as the first argument and an array of string literals as the second argument. Our function should then filter the input ...

Read More

Path with smallest sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 361 Views

This problem involves finding the path through a 2D array that picks exactly one element from each row, where no two adjacent rows can have elements from the same column, and the path has the minimum sum. Problem Statement Given a 2D array, we need to: Pick exactly one element from each row No two elements from adjacent rows can be in the same column Return the minimum sum among all valid paths For example, with the input: const arr = [ [4, 7, 1], ...

Read More

Removing a specific substring from a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 479 Views

We are given a main string and a substring, our job is to create a function, let's say removeString() that takes in these two arguments and returns a version of the main string which is free of the substring. Here, we need to remove the separator from a string, for example: this-is-a-string Using split() and join() Method The most common approach is to split the string by the substring and then join the parts back together: const removeString = (string, separator) => { // we split the string ...

Read More

Detecting the first non-unique element in array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 244 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. How It Works The algorithm works by comparing each element's current index with its last occurrence index using lastIndexOf(). ...

Read More

Reversing vowels in a string JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 446 Views

We are required to write a JavaScript function that takes a string as input and reverse only the vowels of a string. Problem Example If the input string is: const str = 'Hello'; Then the output should be: 'Holle' Notice how 'e' and 'o' swap positions while consonants 'H', 'l', 'l' remain in their original places. Solution Using Two Pointers The most efficient approach uses two pointers from opposite ends of the string: const str = 'Hello'; const reverseVowels = (str = '') => { ...

Read More

Removing already listed intervals in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 220 Views

JavaScript function that takes in a 2-D array, arr, as the first and the only argument. Each subarray of our input array is an array of exactly two numbers, specifying a time interval. Our function should remove all intervals that are covered by another interval in the array arr. Interval [a, b) is covered by interval [c, d) if and only if c { arr.sort(([a, b], [c, d]) => (a === c ? d - b : a - c)); console.log("After sorting:", arr); ...

Read More

How to add Summernote Editor to the webpage in Javascript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 4K+ Views

To add Summernote Editor in a webpage, we first need to include the Summernote CSS and JS files in the head of our HTML document. Next, we need to initialize the Summernote editor on a specific text area or div element by calling the Summernote function on it. Finally, we can customize the editor's options and functionality by passing options as an object to the Summernote function. Let us first understand what Summernote editor is. What is Summernote? Summernote is a JavaScript library that allows for the creation and editing of rich text within a web ...

Read More

Checking for the Gapful numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 162 Views

A gapful number is a special type of number that meets specific criteria. Understanding gapful numbers can be useful in mathematical programming and number theory applications. What is a Gapful Number? A number is considered gapful when: It has at least three digits, and It is exactly divisible by the number formed by combining its first and last digits Examples 1053 is a gapful number because it has 4 digits and is divisible by 13 (first digit 1 + last digit 3). 135 is ...

Read More

Detecting the largest element in an array of Numbers (nested) in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 264 Views

We need to find the largest number in a nested array of any depth. This problem requires recursion to traverse through all nested levels and compare each number to find the maximum value. For example, if we have this nested array: const arr = [ 34, 65, 67, [ 43, 76, 87, 23, 56, 7, [ 54, 7, 87, 23, 79, 994, 2 ], ...

Read More
Showing 3711–3720 of 8,392 articles
« Prev 1 370 371 372 373 374 840 Next »
Advertisements