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
Javascript Articles
Page 146 of 534
Finding minimum deletions in string in JavaScript
When working with binary strings, we often need to ensure no two adjacent characters are the same. This requires finding the minimum number of deletions to create an alternating pattern. Problem Statement Given a binary string, we need to find the minimum deletions required so that no two adjacent characters are identical. const str = '001001'; console.log("Original string:", str); Original string: 001001 For the string '001001', we need 2 deletions to get '0101' (removing characters at indices 0 and 3). Solution Approach The algorithm iterates through the string and ...
Read MoreFinding three elements with required sum in an array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should then pick three such numbers from the array, (if they exist) whose sum is equal to the number specified by the second argument. The function should finally return an array of arrays of all such triplets if they exist, an empty array otherwise. For example − If the input array and the number is − const arr = [2, 5, 7, 8, 9, 11, 1, ...
Read MoreAdding paragraph tag to substrings within a string in JavaScript
We are required to write a JavaScript function that takes in a string str as the first argument and an array of strings, arr as the second argument. We need to add a closed pair of paragraph tag and to wrap the substrings in str that exist in arr. If two such substrings overlap, we need to wrap them together by only one pair of closed paragraph tag. Also, if two substrings wrapped by paragraph tags are consecutive, we need to combine them. For example, if the input string and the array are: const ...
Read MoreChecking if an array is sorted lexicographically in reference to some scrambled alphabet sequence in JavaScript
We need to write a JavaScript function that checks if an array of words is sorted lexicographically according to a custom alphabet order. The function takes two arguments: an array of words and a string representing the custom alphabetical order. The task is to verify whether the words are arranged correctly based on the given alphabet sequence. If they are properly sorted, return true; otherwise, return false. Problem Understanding Consider this example: const arr = ['this', 'is', 'something', 'mad']; const order = 'hdetljnopqabcuvwxfgirsykmz'; In the custom order, 't' comes before 'i', 'i' comes ...
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 MoreValidating a square in a 2-D plane in JavaScript
We are required to write a JavaScript function that takes in four arguments. The four arguments will all be arrays of exactly two numbers representing the coordinates of four vertices of a quadrilateral or any figure (closed or unclosed) on a plane. The task of our function is to determine whether or not the four vertices form a square. If they do form a square, we should return true, false otherwise. Understanding the Problem A square has specific properties: All four sides are equal in length All four angles ...
Read MoreFinding maximum number of consecutive 1's in a binary array in JavaScript
We are required to write a JavaScript function that takes in a binary array (an array that consists of 0 or 1 only) as the only argument. The function should find the length of that consecutive subarray of the array that consists of only 1 and return it. For example − If the input array is − const arr = [1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1]; Then the output should be − 4 We will use the sliding window algorithm to ...
Read MoreFinding minimum steps to make array elements equal in JavaScript
We are required to write a JavaScript function that takes in a number, num as the only argument. The function should first construct an array of n elements based on the following rule: arr[i] = (2 * i) + 1; Therefore, if the input number is 5, then the array should be: const arr = [1, 3, 5, 7, 9]; Our function is supposed to calculate and return the minimum number of steps it should take so that all the elements of the array become equal. Understanding the Problem Let ...
Read MoreFinding even length numbers from an array in JavaScript
We are required to write a JavaScript function that takes in an array of integers as the first and the only argument. The function should then construct and return a new array that contains only those elements from the original array that have an even number of digits. For example, if we have an array with numbers of different digit lengths, we need to filter out only those numbers whose digit count is even (2, 4, 6, etc.). Problem Example If the input array is: const arr = [12, 6, 123, 3457, 234, 2]; ...
Read MoreFinding average of n top marks of each student in JavaScript
Suppose, we have an array of objects that contains information about some students and the marks scored by them over a period of time like this: const marks = [ { id: 231, score: 34 }, { id: 233, score: 37 }, { id: 231, score: 31 }, { id: 233, score: 39 }, { id: 231, score: 44 }, { id: 233, score: 41 }, { id: 231, score: ...
Read More