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
Articles by AmitDiwan
Page 469 of 840
Sum all duplicate value in array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers with duplicate entries and sums all the duplicate entries to one index. For example − If the input array is − const input = [1, 3, 1, 3, 5, 7, 5, 4]; Then the output should be − const output = [2, 6, 7, 10, 4]; Using Map to Track and Sum Duplicates The most efficient approach is to use a Map to count occurrences and then multiply each unique value by its count: ...
Read MoreAdding two values at a time from an array - JavaScript
Let's say, we are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as sum of two consecutive elements from the original array. For example, if the input array is − const arr = [3, 6, 3, 87, 3, 23, 2, 2, 6, 8]; Then the output should be − const output = [9, 90, 26, 4, 14]; Example Following is the code − const arr = [3, 6, 3, 87, 3, 23, 2, 2, 6, 8]; ...
Read MoreHow to check if a document is ready in JavaScript?
In JavaScript, you can check if the DOM is fully loaded using document.readyState or event listeners. This is crucial for ensuring your scripts run after the HTML structure is ready. Understanding document.readyState The document.readyState property has three possible values: "loading" - The document is still loading "interactive" - The document has loaded but resources like images may still be loading "complete" - The document and all resources have finished loading Method 1: Using document.readyState Document Ready ...
Read MoreHow to find a group of three elements in an array whose sum equals some target sum JavaScript
We need to write a function that finds three elements in an array whose sum equals a target value. The function should return the indices of these three elements, or -1 if no such triplet exists. Approach We'll use a two-step approach: Create a twoSum() helper function that finds two numbers adding up to a target sum Iterate through each element and use twoSum() to find the remaining two elements This approach has O(N²) time complexity, where N is the array length. How It Works The twoSum() function uses a hash map ...
Read MoreFind unique and biggest string values from an array in JavaScript
In JavaScript, we often need to filter arrays to get unique values and sort them by specific criteria. This article demonstrates how to find the longest unique string values from an array of objects. Problem Statement Given an array of objects with text properties, we need to create a function that returns n objects with the longest unique string values. If fewer than n unique objects exist, return all unique objects. const arr = [ {text: 'use'}, {text: 'secur'}, {text: 'form'}, ...
Read MoreCreate new array without impacting values from old array in JavaScript?
In JavaScript, directly assigning an array to a new variable creates a reference to the original array, not a copy. This means changes to the new variable will affect the original array. To create a truly independent copy, you need to use specific cloning methods. The Problem with Direct Assignment When you assign an array directly, both variables point to the same array in memory: var originalArray = ["John", "Mike", "Sam", "Carol"]; var newArray = originalArray; // This creates a reference, not a copy newArray.push("David"); console.log("Original array:", originalArray); console.log("New array:", newArray); ...
Read MoreCounting the clusters of positive numbers - JavaScript Arrays
Let's say, we have an array of numbers like this − const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]; We are required to write a JavaScript function that counts the consecutive groups of non-negative (positives and 0) numbers in the array. Like here we have consecutive non-negatives from index 3 to 3 (only one element, but still a cluster) which forms one group and then from index 9 to end of array forms the second group. Therefore, for this array, the function should return 2. How It Works ...
Read MoreCan we re-throw errors in JavaScript? Explain.
Yes, JavaScript allows you to re-throw errors after catching them using the throw statement inside a catch block. This technique is useful for conditional error handling, logging, or passing errors up the call stack. What is Re-throwing? Re-throwing means catching an error, performing some operation (like logging or validation), and then throwing the same or a new error to be handled elsewhere. Basic Syntax try { // Code that may throw an error } catch (error) { // Handle or log the error ...
Read MoreIntersection of two arrays JavaScript
Finding the intersection of two arrays means finding elements that appear in both arrays, preserving the frequency of each element as it appears in both arrays. For example, if we have arr1 = [1, 2, 3, 1] and arr2 = [1, 3, 1], the intersection is [1, 3, 1] because element 1 appears twice in both arrays, and element 3 appears once in both. Problem Statement Given two arrays of numbers, we need to write a function that computes their intersection and returns an array containing the intersecting elements. Each element in the result should appear as ...
Read MoreHow to compare two arrays to see how many same elements they have in JavaScript?
Let's say, we have two arrays, one contains the correct answer strings of some questions and one contains the answers attempted by a candidate, but somehow the arrays got shuffled and now they don't have answers in corresponding order. But we can be sure that no two questions had the same answers. Our job now is to write a function that takes these two arrays, checks them for common elements and finds all the common elements between them and then calculates the marks percentage of the candidate based on the count of common answers. Example const ...
Read More