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 145 of 534
Removing the odd occurrence of any number/element from an array in JavaScript
When working with arrays, you might need to remove elements that appear an odd number of times (excluding elements that appear only once). This tutorial shows how to implement this functionality in JavaScript. Problem Statement Given an array of numbers, we need to remove the last occurrence of any element that appears an odd number of times, but keep elements that appear only once. const arr = [1, 6, 3, 1, 3, 1, 6, 3]; console.log("Original array:", arr); Original array: [1, 6, 3, 1, 3, 1, 6, 3] In this example, ...
Read MoreComparing corresponding values of two arrays in JavaScript
When working with arrays in JavaScript, you might need to compare corresponding elements to determine which array has more "dominant" values at each position. This article demonstrates how to build a function that compares two arrays element by element and returns a result indicating which array has more greater values. Problem Statement Given two arrays of equal length, we need to compare corresponding elements and return: -1 if the first array has more elements greater than their corresponding elements in the second array 1 if the second array has more elements greater than their corresponding elements ...
Read MorePositive, negative and zeroes contribution of an array in JavaScript
In JavaScript, you may need to analyze an array of integers to find the fractional ratio of positive, negative, and zero values. This is useful for statistical analysis and data processing tasks. Problem Statement Given an array of integers containing positive, negative, and zero values: const arr = [23, -1, 0, 11, 18]; We need to write a function that calculates the fractional ratio for each group: negative, zero, and positive numbers. The output should be an array of three decimal values representing these ratios. For the above array with length 5, the ...
Read MoreSum excluding one element in JavaScript
In JavaScript, when you need to find the minimum and maximum possible sums by excluding exactly one element from an array, you can solve this efficiently using a single loop approach. The key insight is that to get the minimum sum (excluding one element), you remove the largest element. To get the maximum sum (excluding one element), you remove the smallest element. Problem Statement Given an array of integers, return an array with two values: First integer: smallest possible sum excluding any one element Second integer: greatest ...
Read MoreFinding desired sum of elements in an array in JavaScript
Finding groups of elements in an array that sum to a desired value is a common programming problem. This involves checking combinations of consecutive elements to match both a target sum and group size. Problem Statement Given an array of numbers, we need to find how many consecutive groups of a specific length sum up to a target value. For example: const arr = [1, 2, 1, 3, 2]; const sum = 3; const num = 2; We want to find groups of 2 consecutive elements that sum to 3. The groups [1, 2] ...
Read MoreFinding a pair that is divisible by some number in JavaScript
We need to write a JavaScript function that finds all pairs of numbers from an array whose sum is divisible by a given number. The function takes an array of numbers and a divisor as parameters. Problem Statement Given an array of numbers and a divisor, find all pairs where: (arr[i] + arr[j]) % divisor === 0, and i < j For example, with array [1, 2, 3, 4, 5, 6] and divisor 4, we need pairs whose sum is divisible by 4. Input and Expected Output Input: const arr = ...
Read MoreFinding matching pair from an array in JavaScript
We are required to write a JavaScript function that takes in an array of integers that might contain some repeating values. Our function should find out the number of pairs of identical integers we can extract out of the array. For example, if the input array is: const arr = [1, 5, 2, 1, 6, 2, 2, 9]; Then the output should be: const output = 2; because the desired pairs are (1, 1) and (2, 2). Note that we have three 2's, but we can only form one pair from ...
Read MoreLongest string with two distinct characters in JavaScript
We need to write a JavaScript function that finds the longest substring containing at most two distinct characters. The function takes a string as input and returns the length of the longest valid substring. For example, if the input string is 'kjeljsdl', the longest substring with at most 2 distinct characters is 'jljl' with length 4. Understanding the Problem A substring with at most two distinct characters means we can have: Only one character repeated: "aaa" Two different characters in any combination: "abab", "aabb" Sliding Window Approach The most efficient solution uses a ...
Read MoreSubstring combination in JavaScript
We are required to write a JavaScript function that takes in two strings as the first and the second argument. Let's call these strings str1 and str2. The function should check whether there exists a substring combination in str2, that when combined yields str2. By substring combination, we mean that we can skip characters but we have to maintain the order of the characters selected from str1. For example, if the input strings are: const str1 = 'desxooajmepwele'; const str2 = 'example'; Then the output should be true because the string 'example' can be ...
Read MoreDetermining beautiful number string in JavaScript
A numeric string is called a beautiful string if it can be split into a sequence of two or more positive integers, satisfying the following conditions: arr[i] - arr[i - 1] = 1, for any i in the sequence, i.e., each element is exactly one more than the previous element. No element should contain a leading zero. For example, '50607' can be split into [5, 06, 07], but it's not beautiful because 06 and 07 have leading zeros. The sequence order cannot be rearranged. Example For ...
Read More