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 460 of 534
Find the difference of largest and the smallest number in an array without sorting it in JavaScript
We have an array of Numbers that are arranged in pure random order. Our job is to write a function that takes in one such array of Numbers and returns the difference of greatest and smallest numbers present in it, but without sorting the array. We will use the Array.prototype.reduce() function to pick the smallest and greatest numbers from the array and later will return their difference. Using Array.reduce() Method The reduce() method allows us to iterate through the array once and track both maximum and minimum values simultaneously: const arr = [23, 65, 67, ...
Read MoreSmallest positive value that cannot be represented as sum of subarray JavaScript
We have a sorted array of positive integers like this: const arr = [1, 3, 6, 10, 11, 15]; We are required to write a function, say findSmallest() that takes in one such array and returns the smallest positive integer that cannot be represented as the sum of some subarray of this original array. For example, for the array written above, 2 is the smallest positive integer which cannot be reached by summing any subarray of this original array. Algorithm Approach Since the array is sorted, we can achieve the solution to this ...
Read MoreRemove all characters of first string from second JavaScript
When working with strings in JavaScript, you may need to remove all characters from one string that appear in another string. This is useful for filtering, data cleaning, or text processing tasks. Let's say we have two strings with characters in no specific order. We need to write a function that takes these two strings and returns a modified version of the second string with all characters from the first string removed. Example Strings const first = "hello world"; const second = "hey there"; Using Array Methods (Recommended) The most straightforward approach uses ...
Read MoreJavaScript Determine the array having majority element and return TRUE if its in the same array
We are required to write a JavaScript function that takes in an array of numbers with repetitive values and returns the number that appears for more than (n/2) number of times where n is the length of the array. If there is no such element in the array, our function should return false. This problem is commonly known as finding the "majority element" in an array. A majority element is one that appears more than half the time in the array. Algorithm Approach We'll use the Boyer-Moore Voting Algorithm, which works in two phases: ...
Read MoreReturn TRUE if the first string starts with a specific second string JavaScript
We are required to write a JavaScript function that takes in two strings and checks whether the first string starts with the second string or not. For example: If the two strings are: "Disaster management report" "Disas" Then our function should return true There are multiple ways to check if a string starts with another string in JavaScript. Let's explore the most common approaches. Using startsWith() Method (Recommended) The startsWith() method is the built-in and most straightforward way to check if a string starts with another string: const first = 'The ...
Read MoreCheck for Subarray in the original array with 0 sum JavaScript
We are required to write a JavaScript function that takes in an array of Numbers with some positive and negative values. We are required to determine whether there exists a subarray in the original array whose net sum is 0 or not. Our function should return a boolean on this basis. Approach The approach here is simple. We iterate over the array using a for loop, calculate the cumulative sum up to that particular element. And if any point the cumulative becomes 0 or attains a value it has previously attained, then there exists a subarray with ...
Read MoreSplit string into equal parts JavaScript
In JavaScript, splitting a string into equal parts can be accomplished in several ways. This article demonstrates how to split a string into n equal parts using an alternating pattern that takes characters from both ends of the string. Problem Statement We need to write a JavaScript function that takes a string and a number n as arguments, where n exactly divides the string length. The function should return an array of n strings of equal length, formed by alternating between the first and last characters of the remaining string. For example: If the string ...
Read MoreJavaScript R- eturn Array Item(s) With Largest Score
We have an array of arrays that contains the marks scored by some students in different subjects. We need to write a function that returns the top scorer(s) for each subject, handling cases where multiple students have the same highest score. Problem Setup Given this input data: const arr = [ ['Math', 'John', 100], ['Math', 'Jake', 89], ['Math', 'Amy', 93], ['Science', 'Jake', 89], ['Science', 'John', 89], ['Science', 'Amy', 83], ...
Read MoreRearrange string so that same character become n distance apart JavaScript
We need to rearrange a string so that identical characters are exactly n positions apart from each other. This problem requires careful character placement to maintain the specified distance constraint. For example, if we have the string "accessories" and n = 3, we need to ensure that each 's' character is exactly 3 positions away from other 's' characters, and the same applies to other repeated characters. Problem Understanding The algorithm works by: Counting the frequency of each character Sorting characters by frequency (most frequent first) Placing characters in a round-robin fashion with n distance apart ...
Read MoreObject difference in JavaScript
In JavaScript, finding the difference between two objects means identifying keys that exist in the first object but not in the second. This is useful for comparing data structures, tracking changes, or filtering object properties. Problem Statement We need to write a JavaScript function that takes two objects (possibly nested) and returns a new object containing only the key-value pairs that exist in the first object but are missing from the second object. Basic Implementation Here's a function that compares two objects and returns the difference: const obj1 = { "firstName": ...
Read More