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 307 of 840
Is the reversed number a prime number in JavaScript
We are required to write a JavaScript function that takes in a number and returns true if the reverse of that number is a prime number, false otherwise. Problem Understanding To solve this problem, we need two helper functions: A function to reverse the digits of a number A function to check if a number is prime Solution Implementation Here's the complete solution with helper functions: const num = 13; const findReverse = (num) => { return +num .toString() .split('') .reverse() .join(''); }; const isPrime = (num) => { if (num
Read MoreReversing the even length words of a string in JavaScript
We are required to write a JavaScript function that takes in a string and reverses the words in the string that have an even number of characters in them. Let's say the following is our string: const str = 'This is an example string'; We want to reverse the even length words of the above string i.e. reverse the following words: This (4 characters) is (2 characters) an (2 characters) string (6 characters) The word "example" has 7 characters (odd length), so it remains unchanged. Syntax ...
Read MoreUsing one array to help filter the other in JavaScript
When working with arrays of objects, you often need to filter one array based on values from another array. This is a common requirement when you have a list of items and want to keep only those that match specific criteria. Problem Statement Given an array of objects and an array of values, we need to filter the objects array to include only those objects whose property matches values in the second array. Sample Data Let's start with these arrays: const main = [ {name: "Karan", age: 34}, {name: "Aayush", ...
Read MoreAlternative sorting of an array in JavaScript
We need to write a JavaScript function that sorts an array in an alternating pattern where elements follow the sequence: smaller, larger, smaller, larger, and so on. The pattern we want to achieve is: arr[0] < arr[1] > arr[2] < arr[3] > arr[4] < arr[5]... This creates a zigzag or wave-like pattern in the sorted array. There can be multiple valid solutions for any given input array. Example Input and Output For an input array: const arr = [1, 2, 3, 4, 5, 6]; One possible output could be: ...
Read MoreFinding the longest non-negative sum sequence using JavaScript
Problem We are required to write a JavaScript function that takes in an array containing a sequence of integers, each element of which contains a possible value ranging between -1 and 1. Our function should return the size of the longest sub-section of that sequence with a sum of zero or higher. Understanding the Algorithm The solution uses a prefix sum approach with an index mapping technique. It tracks cumulative sums and uses an array to store the first occurrence of each sum value, allowing efficient calculation of subarray lengths. Example Following is the ...
Read MoreDifferences in two strings in JavaScript
We are required to write a JavaScript function that takes in two strings and find the number of corresponding dissimilarities in the strings. The corresponding elements will be dissimilar if they are not equal at the same position. Example Strings Let's say the following are our strings: const str1 = 'Hello world!!!'; const str2 = 'Hellp world111'; In this case, we need to compare each character at the same index and count how many positions have different characters. Method 1: Using a For Loop const str1 = 'Hello world!!!'; const str2 ...
Read MoreFinding intersection of arrays that contain repetitive entries in JavaScript
Finding the intersection of arrays with repetitive entries means identifying common elements between two arrays while preserving duplicate occurrences. This is different from a simple set intersection, as we need to consider the frequency of each element. The function should build a third array based on the two input arrays that contains all the elements that are common to both arrays. If there are multiple instances of the same element present in both arrays, we need to include all such instances up to the minimum count found in either array. Problem Example If the input arrays are: ...
Read MoreMerging two sorted arrays into one sorted array using JavaScript
We are required to write a JavaScript function that takes in two sorted arrays of numbers. Our function should merge all the elements of both arrays into a new array and return that new array sorted in the same order. Two-Pointer Approach The most efficient approach uses two pointers to traverse both arrays simultaneously, comparing elements and adding the smaller one to the result array. const arr1 = [1, 3, 4, 5, 6, 8]; const arr2 = [4, 6, 8, 9, 11]; const mergeSortedArrays = (arr1 = [], arr2 = []) => { ...
Read MoreFinding the index of the first repeating character in a string in JavaScript
We are required to write a JavaScript function that takes in a string and returns the index of the first character that appears twice in the string. If there is no such character then we should return -1. Let's say the following is our string − const str = 'Hello world, how are you'; We need to find the index of the first repeating character. Understanding the Problem In the string "Hello world, how are you", we need to find which character repeats first. The character 'l' appears at index 2 and again ...
Read MoreFormatting a string to separate identical characters in JavaScript
In JavaScript, we often need to rearrange string characters so that no two identical characters are adjacent to each other. This is a classic string manipulation problem that requires careful character distribution. The function should reorganize the characters in the input string such that no two identical characters are placed next to each other. If such an arrangement is possible, it returns the rearranged string; otherwise, it returns an empty string. Problem Understanding Given a string like 'add', we need to rearrange it to 'dad' where no two identical characters are adjacent. The key insight is that ...
Read More