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
Web Development Articles
Page 266 of 801
Merging and rectifying arrays in JavaScript
We need to create a JavaScript function that merges two arrays and removes duplicates, ensuring each element appears only once in the final result. Problem We are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2, as the first and the second arguments. Our function should merge the elements of both these arrays into a new array and if upon merging or before merging there exists any duplicates, we should delete the excess duplicates so that only one copy of each element is present in the merged array. The ...
Read MoreFiltering out numerals from string in JavaScript
We are required to write a JavaScript function that takes in a string, str, which contains a combination of alphabets, special characters and numbers. Our function should return a new string based on the input string that contains only the numbers present in the string str, maintaining their relative order. For example, if the input to the function is: const str = 'revd1fdfdfs2v34fd5gfgfd6gffg7ds'; Then the output should be: '1234567' Using for Loop and Type Coercion This approach iterates through each character and uses the unary plus operator (+) to ...
Read MoreLongest subarray with unit difference in JavaScript
We need to find the length of the longest subarray where the difference between maximum and minimum values is exactly 1. This means the subarray can only contain two consecutive numbers (like 3 and 4) or just one unique number repeated. Problem Statement Given an array of numbers, find the length of the longest subarray where the difference between its maximum and minimum values is exactly 1. For example, with input array: const arr = [2, 4, 3, 3, 6, 3, 4, 8]; The longest valid subarray is [4, 3, 3, 3, 4] ...
Read MoreLength of shortest unsorted array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and finds the length of the shortest continuous subarray that, when sorted, makes the entire array sorted in ascending order. Our function needs to find the length of one continuous subarray such that if we only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. Problem Example For example, if the input array is: const arr = [3, 7, 5, 9, 11, 10, 16]; console.log("Original array:", arr); Original array: ...
Read MoreReshaping 2-D array in JavaScript
In JavaScript, reshaping a 2-D array means converting it to a new matrix with different dimensions while preserving the original element order. This is useful for data manipulation and matrix operations. Problem Statement We need to write a JavaScript function that takes a 2-D array and reshapes it into a new matrix with specified rows and columns. The elements should maintain their row-traversing order from the original array. For example, if we have: const arr = [ [6, 7], [8, 9] ]; const r = 1, c ...
Read MoreFind and return the longest length of set in JavaScript
In JavaScript, finding the longest length of a set involves traversing array elements in a cycle pattern where each element points to the next index. This problem requires tracking visited elements to avoid infinite loops and find the maximum cycle length. Problem Statement Given an array of length N containing all integers from 0 to N-1, we need to find the longest set S where S[i] = {A[i], A[A[i]], A[A[A[i]]], ...}. We start from index i, then move to A[i], then to A[A[i]], and continue until we encounter a duplicate element. Example Input and Output For ...
Read MoreFinding minimum time difference in an array in JavaScript
We are required to write a JavaScript function that takes in an array of 24-hour clock time points in "Hour:Minutes" format. Our function should find the minimum minutes difference between any two time points in the array. For example, if the input to the function is: const arr = ["23:59", "00:00"]; Then the output should be 1 minute, because the minimum difference between these times is 1 minute (from 23:59 to 00:00). Understanding the Problem The key challenge is handling the circular nature of time. When comparing "23:59" and "00:00", we need to ...
Read MoreCreating a chained operation class in JavaScript
This article demonstrates how to create a fluent interface class in JavaScript that allows chaining numeric values and mathematical operations. The Streak class enables natural language-like mathematical expressions. Problem We need to create a custom data type Streak in JavaScript that supports method chaining with values and operations alternately. The supported values are: → one, two, three, four, five, six, seven, eight, nine The supported operations are: → plus, minus For example, this expression: Streak.one.plus.five.minus.three; Should evaluate to: 3 How It ...
Read MoreFinding all peaks and their positions in an array in JavaScript
A peak (local maximum) in an array is an element that is greater than both its neighbors. Finding all peaks and their positions is useful for data analysis, signal processing, and identifying trends. Problem Statement Given an array of integers, we need to find all local maxima (peaks) and return an object containing two arrays: maximas (the peak values) and positions (their indices). Consider this array: const arr = [4, 3, 4, 7, 5, 2, 3, 4, 3, 2, 3, 4]; console.log("Array:", arr); Array: [4, 3, 4, 7, 5, 2, 3, 4, ...
Read MoreFinding the final direction of movement in JavaScript
Problem We need to write a JavaScript function that takes an array of directional characters and finds the final direction after canceling out opposite movements. The array contains only 4 possible characters: 'N' → stands for North direction 'S' → stands for South direction 'W' → stands for West direction 'E' → stands for East direction Each character represents a unit move in that direction. When opposite directions ('S' and 'N') or ('E' and 'W') appear adjacently, they cancel each other out. Our ...
Read More