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
Object Oriented Programming Articles
Page 51 of 589
Subset with maximum sum in JavaScript
In JavaScript, finding the subset of non-adjacent elements with maximum sum is a classic dynamic programming problem. We need to decide whether to include each element or skip it, ensuring no two adjacent elements are selected. The key insight is that for each element, we have two choices: include it (and skip the previous element) or exclude it (and take the maximum sum up to the previous element). Problem Statement Given an array of integers, find the subset of non-adjacent elements that produces the maximum sum. Adjacent elements cannot be selected together. For example, with the ...
Read MoreCheck if the string is a combination of strings in an array using JavaScript
We are required to write a JavaScript function that takes in an array of strings as the first argument and a string as the second argument. The function should check whether the string specified by second argument can be formed by combining the strings of the array in any possible way. For example − If the input array is − const arr = ["for", "car", "keys", "forth"]; And the string is − const str = "forthcarkeys"; Then the output should be true, because the string is a combination of elements ...
Read MoreHow to build a string with no repeating character n separate list of characters? in JavaScript
When working with multiple arrays of characters, we often need to create combinations where each string contains exactly one character from each array with no duplicate characters. This is useful for generating unique combinations while avoiding repeated letters that might exist across different arrays. Problem Overview Given multiple arrays of characters, we need to build all possible strings that: Contains exactly one letter from each array ...
Read MoreSplit number into 4 random numbers in JavaScript
We need to write a JavaScript function that takes a total number and a maximum value, then generates four random numbers that sum to the total while ensuring none exceeds the maximum. The function should generate four random numbers where: All four numbers sum to the given total No number exceeds the specified maximum Repetition of numbers is allowed For example, if the total is 10 and maximum is 4, a valid output could be [3, 2, 3, 2]. Algorithm Overview The algorithm works by: Generating random values between 0 and 1 Scaling ...
Read MoreHow to check if an array contains integer values in JavaScript ?
In JavaScript, checking if an array contains integer values requires understanding the difference between numbers and strings that look like numbers. This article explores different approaches to detect actual integer values in arrays. The Problem with String Numbers Arrays often contain string representations of numbers like "123" instead of actual numbers. We need to distinguish between these types. const mixedArray = ["123", 45, "hello", 67.5, "89"]; console.log(typeof "123"); // "string" console.log(typeof 45); // "number" string number Method 1: Using typeof and Number.isInteger() The most ...
Read MoreHow to convert nested array pairs to objects in an array in JavaScript ?
When working with complex data structures, you often need to convert nested array pairs into objects. This is common when processing form data or API responses that use key-value pair arrays. Problem Statement Suppose we have an array of arrays like this: const arr = [ [ ['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk'] ], ...
Read MoreSort an array of objects by multiple properties in JavaScript
Sorting an array of objects by multiple properties is a common requirement in JavaScript applications. This involves creating custom comparison logic that prioritizes different properties based on specific criteria. Suppose, we have an array of objects like this: const arr = [ { id: 1, score: 1, isCut: false, dnf: false }, { id: 2, score: 2, isCut: false, dnf: false }, { id: 3, score: 3, isCut: false, dnf: false }, { id: 4, score: 4, isCut: false, dnf: false ...
Read MoreHighest occurrence in an array or first selected in JavaScript
We are required to write a JavaScript function that takes in an array of literal values. Our function should then return the highest occurrence of an array value, and if there's an equal occurrence, we should return the first selected value of the equal occurrences. Problem Statement Given an array of values, find the element that appears most frequently. If multiple elements have the same highest frequency, return the one that appears first in the array. const arr1 = ['25', '50', 'a', 'a', 'b', 'c']; // 'a' appears 2 times (most frequent), so return 'a' ...
Read MoreFind all substrings combinations within arrays in JavaScript
We are required to write a JavaScript function that takes in an array of strings. The function should find all the substring and superstring combinations that exist in the array and return an array of those elements. For example − If the array is − const arr = ["abc", "abcd", "abcde", "xyz"]; Then the output should be − const output = ["abc", "abcd", "abcde"]; because the first two are the substring of the last one. How It Works The algorithm compares each string with every other string in the ...
Read MoreReturn a sorted array in lexicographical order in JavaScript
We are required to write a JavaScript function that takes two arrays, say arr1 and arr2. Our function should return a sorted array in lexicographical order of the strings of arr1 which are substrings of strings of arr2. How It Works The function iterates through arr1 and checks if each string is a substring of any string in arr2. If found, it adds the string to the result array and continues to the next string using a labeled continue statement. Example The code for this will be − const lexicographicalSort = (arr1 = [], ...
Read More