AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 347 of 840

Can array be divided into n partitions with equal sums in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 301 Views

We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a number, num, as the second argument. The function should determine whether there exists a way of distributing the elements of the array arr into num groups such that all the groups have equal sum. If there exists any such way, our function should return true, false otherwise. For example − If the input array and the number are − const arr = [4, 6, 3, 3, 7, 4, 1]; const num = 4; ...

Read More

Sum of all positives present in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

We are required to write a JavaScript function that takes in an array of numbers (positive and negative). Our function should calculate and return the sum of all the positive numbers present in the array. Method 1: Using reduce() with Helper Function This approach uses a helper function to check if a number is positive and reduce() to accumulate the sum: const arr = [5, -5, -3, -5, -7, -8, 1, 9]; const sumPositives = (arr = []) => { const isPositive = num => typeof num === 'number' && num ...

Read More

Reversing a string while maintaining the position of spaces in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 873 Views

Problem We are required to write a JavaScript function that takes in a string that might contain some spaces. Our function should reverse the non-space characters while keeping spaces in their original positions. Understanding the Task The goal is to reverse only the alphabetic characters while maintaining the exact positions of spaces. For example, "this is normal string" becomes "gnir ts lamron sisiht" - spaces remain at positions 4, 7, and 14. Example Following is the code: const str = 'this is normal string'; const reverseWordsWithin = (str = '') => { ...

Read More

JavaScript Array: Checking for multiple values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 286 Views

We are required to write a JavaScript function that takes in two arrays of Numbers and checks whether all the elements of the first array exist in the second or not. Example The code for this will be — const arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89]; const multipleIncludes = (first, second) => { const indexArray = first.map(el => { return second.indexOf(el); }); return indexArray.indexOf(-1) === -1; } console.log(multipleIncludes(arr1, ...

Read More

Function to check two strings and return common words in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 895 Views

We are required to write a JavaScript function that takes in two strings as arguments. The function should then check the two strings for common substrings and prepare an array of those common parts. The function will find all possible substrings from the first string and check if they exist in the second string, returning an array of matches. Example const str1 = "IloveLinux"; const str2 = "weloveNodejs"; const findCommon = (str1 = '', str2 = '') => { const common = Object.create(null); let i, j, part; for (i = 0; i < str1.length - 1; i++) { for (j = i + 1; j

Read More

Regular Expression Matching in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 340 Views

Regular expression matching is a fundamental concept in computer science where we match an input string against a pattern containing special characters. In JavaScript, we can implement custom regex matching using dynamic programming to handle patterns with . (any character) and * (zero or more occurrences). Problem Definition Given an input string str and a pattern p, implement regular expression matching with support for: . → Matches any single character * → Matches zero or more of the preceding element The matching must cover the entire input string ...

Read More

Counting the number of palindromes that can be constructed from a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 415 Views

We are required to write a JavaScript function that takes in a string of characters as the first argument, say str, and a number, say num, as the second argument. The function should count the number of palindrome strings all exactly of length num can be constructed from the given string str. The function should then finally return the count. Problem Example If the input string and the number is: const str = 'ij'; const num = 4; Then the output should be: 4 because those four possible palindrome ...

Read More

Finding n most frequent words from a sentence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

For the purpose of this question, we define a sentence as a string that contains English alphabets and punctuations and a word is a substring of that sentence joined together by whitespaces. We are required to write a JavaScript function that takes in a sentence string, str, as the first argument and a number, num, as the second argument. The function should first count the frequency of each word in the sentence and then return an array of length num containing num most frequent words placed according to decreasing frequencies. Problem Example If the input sentence and ...

Read More

Removing consecutive duplicates from strings in an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Problem We are required to write a JavaScript function that takes in an array of strings. Our function should remove the duplicate characters that appear consecutively in the strings and return the new modified array of strings. Example Following is the code − const arr = ["kelless", "keenness"]; const removeConsecutiveDuplicates = (arr = []) => { const map = []; const res = []; arr.map(el => { el.split('').reduce((acc, value, index, arr) => { ...

Read More

Consecutive elements sum array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 574 Views

We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as the sum of two consecutive elements from the original array. For example, if the input array is − const arr1 = [1, 1, 2, 7, 4, 5, 6, 7, 8, 9]; Then the output should be − const output = [2, 9, 9, 13, 17] How It Works The function pairs consecutive elements: (1+1=2), (2+7=9), (4+5=9), (6+7=13), (8+9=17). If the array has an odd length, the last ...

Read More
Showing 3461–3470 of 8,392 articles
« Prev 1 345 346 347 348 349 840 Next »
Advertisements