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 336 of 840
Similarities between different strings in JavaScript
In JavaScript, finding similarities (intersections) between two arrays of strings is a common operation. We need to write a function that computes the intersection of two string arrays and returns elements that appear in both arrays. Problem Statement Given two arrays of strings, find the common elements that exist in both arrays. Each element in the result should appear as many times as it shows in both arrays. Example If we have these input arrays: arr1 = ['hello', 'world', 'how', 'are', 'you']; arr2 = ['hey', 'world', 'can', 'you', 'rotate']; The expected output ...
Read MoreFind the smallest sum of all indices of unique number pairs summing to a given number in JavaScript
This tutorial shows how to find pairs of numbers in an array that sum to a target value, then return the sum of all indices of these unique pairs. Problem Description Given an array of numbers and a target sum, we need to: Find all pairs of numbers that sum to the target Ensure each number is used only once across all pairs Return the sum of indices of all numbers used in valid pairs Example Walkthrough For array [1, 4, 2, 3, 0, 5] with target sum 7: Valid pairs: 4 ...
Read MoreMap anagrams to one another in JavaScript
One array is an anagram of another if we can rearrange the elements of that array to achieve the other array. For example: [1, 2, 3] and [2, 1, 3] are anagrams of each other. We need to write a JavaScript function that takes two anagram arrays and returns a mapping array. The mapping array contains the index of each element from the first array as it appears in the second array. Problem Example If the two input arrays are: const arr1 = [23, 39, 57, 43, 61]; const arr2 = ...
Read MoreReturning number with increasing digits. in JavaScript
We are required to write a JavaScript function that takes in a number n. Our function should return it with its digits in descending order. Essentially, we should rearrange the digits to create the highest possible number. Problem Given a number, we need to arrange its digits in descending order to form the largest possible number from those digits. Solution The approach is to convert the number to a string, split it into individual digits, sort them in descending order, and then convert back to a number. Example const num = 5423267; ...
Read MoreEncrypting a string based on an algorithm in JavaScript
We are required to write a JavaScript function that takes in a string and encrypts it based on the following algorithm: The string contains only space separated words. We need to encrypt each word in the string using the following rules: The first letter needs to be converted to its ASCII code. The second letter needs to be switched with the last letter. ...
Read MoreUsing operations to produce desired results in JavaScript
We are required to write a JavaScript function that takes in an array of exactly 4 numbers, arr, as the first argument and a target as the second argument. Our function needs to determine whether the numbers in the array arr can be operated through *, /, +, -, (, ) to get a value equal to the target. Problem Example For example, if the input to the function is: Input const arr = [5, 3, 2, 1]; const target = 4; Output true Output Explanation Because ...
Read MoreHow to access an object having spaces in the object's key using JavaScript?
When object keys contain spaces, you cannot use dot notation to access them. Instead, you must use bracket notation with quotes around the key name. The Problem with Spaces in Keys Dot notation requires valid JavaScript identifiers, which cannot contain spaces. Keys with spaces need bracket notation. const person = { 'first name': 'John', 'last name': 'Doe', age: 30 }; // This won't work - syntax error // console.log(person.first name); // This works - bracket notation console.log(person['first name']); console.log(person['last name']); ...
Read MoreRead by key and parse as JSON in JavaScript
In JavaScript, you often need to group JSON data by specific keys and transform the structure. This tutorial shows how to read data by key values and reorganize it into a grouped JSON format. Problem Statement Given a JSON array with nested data, we want to group items by a specific key (like "W") and create a new structure where each group becomes a separate object. Starting with this data structure: const arr = [{ "data": [ { "W": 1, "A1": "123" }, ...
Read MoreIntersection of three sorted arrays in JavaScript
We are required to write a JavaScript function that takes in three arrays of integers all sorted in an increasing order. The function should then construct and return an array that contains only those elements that are present in all three arrays. For example, if the input arrays are: const arr1 = [4, 7, 8, 11, 13, 15, 17]; const arr2 = [1, 3, 4, 13, 18]; const arr3 = [2, 4, 7, 8, 9, 10, 13]; Then the output should be: const output = [4, 13]; Three-Pointer Approach Since ...
Read MoreMap Sum Pairs in JavaScript
The MapSum data structure allows you to store key-value pairs and efficiently calculate the sum of all values whose keys start with a given prefix. This problem is commonly solved using a Trie (prefix tree) data structure. Problem Overview We need to implement a MapSum class with two methods: insert(key, value): Stores a key-value pair. If the key already exists, it updates the value. sum(prefix): Returns the sum of all values whose keys start with the given prefix. Solution Using Trie Structure The solution uses a Trie where each node can store a ...
Read More