Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by AmitDiwan
Page 366 of 840
Summing up to amount with fewest coins in JavaScript
The coin change problem is a classic dynamic programming challenge where we need to find the minimum number of coins required to make a specific amount. If the amount cannot be achieved with the given coin denominations, we return -1. Problem Statement We need to write a JavaScript function that takes two parameters: arr: An array containing different coin denominations amount: The target amount we want to achieve The function should return the minimum number of coins needed to sum up to the target amount. Example Input and Output const arr ...
Read MoreRemoving letters to make adjacent pairs different using JavaScript
We are required to write a JavaScript function that takes in a string containing only 'A', 'B' and 'C'. Our function should find the minimum number of characters needed to be removed from the string so that no two adjacent characters are the same. Problem Statement Given a string with characters 'A', 'B', and 'C', we need to remove the minimum number of characters to ensure all adjacent pairs are different. For example, in "AAB", we need to remove one 'A' to get "AB". Algorithm Approach The strategy is to iterate through the string and whenever ...
Read MoreHow to sort date array in JavaScript
Sorting arrays of dates is a common task in JavaScript. This tutorial shows how to sort an array containing date strings in ascending chronological order. Suppose we have an array that contains some dates like this: const arr = [ [ '02/13/2015', 0.096 ], [ '11/15/2013', 0.189 ], [ '05/15/2014', 0.11 ], [ '12/13/2013', 0.1285 ], [ '01/15/2013', 0.12 ], [ '01/15/2014', 0.11 ], [ '02/14/2014', 0.11 ], ...
Read MoreDistance between 2 duplicate numbers in an array JavaScript
We are required to write a JavaScript function that takes in an array of numbers that contains at least one duplicate pair of numbers. Our function should return the distance between all the duplicate pairs of numbers that exist in the array. The distance is calculated as the minimum difference between indices where duplicate numbers appear. Example Let's work with an array containing duplicates and find the minimum distance between each duplicate pair: const arr = [2, 3, 4, 2, 5, 4, 1, 3]; const findDistance = arr => { var ...
Read MoreFinding sum of all unique elements in JavaScript
In JavaScript, you can sum duplicate elements by counting their occurrences and multiplying each unique value by its count. This technique is useful for consolidating arrays with repeated values. For example, if the input array is: const input = [1, 3, 1, 3, 5, 7, 5, 4]; The output should be: [2, 6, 10, 7, 4] This means: 1 appears 2 times (1×2=2), 3 appears 2 times (3×2=6), 5 appears 2 times (5×2=10), while 7 and 4 appear once each. Using Map to Count and Sum The most efficient ...
Read MoreSet the value in local storage and fetch – JavaScript?
JavaScript's localStorage provides a simple way to store data persistently in the user's browser. Use localStorage.setItem() to store values and localStorage.getItem() to retrieve them. Syntax // Set a value localStorage.setItem("keyName", "value"); // Get a value let value = localStorage.getItem("keyName"); // Remove a value localStorage.removeItem("keyName"); // Clear all localStorage data localStorage.clear(); Setting Values in localStorage The setItem() method accepts two parameters: a key name and the value to store. Values are always stored as strings. ...
Read MoreDeep Search JSON Object JavaScript
Deep searching a JSON object means recursively traversing through all nested objects and arrays to find elements that match specific criteria. This is useful when you need to locate objects buried deep within complex data structures. The Problem Suppose we have the following nested JSON object: const obj = { id: 1, title: 'hello world', child: { id: null, title: 'foobar', ...
Read MoreConstruct an identity matrix of order n in JavaScript
An identity matrix is a square matrix where all diagonal elements are 1 and all other elements are 0. This type of matrix is fundamental in linear algebra and has the property that when multiplied with any matrix, it returns the original matrix unchanged. What is an Identity Matrix? An identity matrix of order n is an n × n square matrix where: All diagonal elements (where row index equals column index) are 1 All other elements are 0 For example, an identity matrix of order 3 will be: [ [1, ...
Read MoreKit-Kat array in JavaScript
In JavaScript, a Kit-Kat array is a custom array where numbers are replaced with specific strings based on divisibility rules. This pattern is similar to the classic FizzBuzz problem but with customizable divisors. Problem Statement Create a function that takes three parameters: a natural number num, and two divisors m and n. The function should return an array containing numbers from 1 to num with these replacements: Replace multiples of m with 'kit' Replace multiples of n with 'kat' Replace multiples of both m ...
Read MoreFinding maximum number from two arrays in JavaScript
We need to create a JavaScript function that takes two arrays of single-digit numbers and returns a new array representing the maximum possible number of a specified length. The function must preserve the relative order of elements from each original array. Problem Statement Given two arrays representing numbers and a target length num, we need to: Select digits from both arrays to form the largest possible number Maintain the relative order within each array Return exactly num digits Example Input and Output const arr1 = [1, 3, 4, 5, 6]; const arr2 ...
Read More