AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 287 of 840

Finding the first unique element in a sorted array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 616 Views

Suppose we have a sorted array of literals like this: const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9]; We are required to write a JavaScript function that takes in one such array and returns the first number that appears only once in the array. If there is no such number in the array, we should return false. For this array, the output should be 6. Using Index Comparison Method Since the array is sorted, we can check if an element is unique by comparing it with ...

Read More

Group all the objects together having the same value for the '_id' key in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 405 Views

Suppose we have an array of objects like this − const arr = [ {_id : "1", S : "2"}, {_id : "1", M : "4"}, {_id : "2", M : "1"}, {_id : "" , M : "1"}, {_id : "3", S : "3"} ]; We are required to write a JavaScript function that takes in one such array and groups all the objects together that have the same value for the '_id' key. Therefore, the final output should ...

Read More

Nested collection filter with JavaScript

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

Filtering nested collections in JavaScript requires searching through objects that contain arrays of other objects. Let's explore how to filter an array based on properties within nested objects. The Problem Suppose we have an array of nested objects where each object contains a legs array with carrier information: const arr = [{ id: 1, legs:[{ carrierName:'Pegasus' }] }, { id: 2, legs:[{ carrierName: 'SunExpress' }, ...

Read More

Removing the odd occurrence of any number/element from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 342 Views

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 More

Removing the second number of the pair that add up to a target in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 175 Views

Problem We need to write a JavaScript function that takes an array of numbers and a target sum. The function should remove the second number from any consecutive pair of numbers that add up to the target. Example Let's see how this works with a practical example: const arr = [1, 2, 3, 4, 5]; const target = 3; const removeSecond = (arr = [], target = 1) => { const res = [arr[0]]; for(let i = 1; i < arr.length; i++){ ...

Read More

How to create a third object from two objects using the key values in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 256 Views

In JavaScript, you can create a third object from two objects by mapping key values between them. This is useful when you have one object containing arrays of keys and another object with corresponding values. Problem Statement Given two objects where the first contains arrays of keys and the second contains key-value pairs, we need to calculate totals for each category: const obj1 = { positive: ['happy', 'excited', 'joyful'], negative: ['depressed', 'sad', 'unhappy'] }; const obj2 = { happy: 6, excited: 1, unhappy: 3 }; ...

Read More

Return the greatest possible product of n numbers from the array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 164 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n, as the second argument. Our function should calculate and return the greatest possible product of n numbers from the array. Problem Analysis To find the maximum product of n numbers, we need to consider both positive and negative numbers. Two negative numbers multiplied together give a positive result, so the strategy involves: Sorting the array to identify the largest and smallest values Choosing ...

Read More

Converting 12 hour Format Time to 24 hour Format in JavaScript

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

Converting 12 hour format time to 24 hour format in JavaScript can be easily achieved using various approaches which we will be discussing in this article. For conversion from 12 hour to 24 hour format we check for the modifier (AM or PM). Depending on the modifier we convert the time formats. In this article, we have specified a time at the beginning in 12 hour format. Our task is to convert 12 hour format time to 24 hour format in JavaScript. Approaches to Convert 12-hour Format to 24-hour Here is a list of approaches to convert ...

Read More

Finding the immediate bigger number formed with the same digits in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 371 Views

We need to write a JavaScript function that takes a number and rearranges its digits to form the smallest number that is just bigger than the input number using the same digits. For instance, if the input number is 112, the output should be 121. If no bigger number can be formed (like 321), we return -1. Algorithm Approach The brute force approach involves: Find the maximum possible number by sorting digits in descending order Check each number from input+1 to maximum Return the first number that uses the same digits Example Implementation ...

Read More

Sum of distinct elements of an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 281 Views

Suppose, we have an array of numbers like this − const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1]; We are required to write a JavaScript function that takes in one such array and counts the sum of all distinct elements of the array. For example: The output for the array mentioned above will be − 30 Method 1: Using lastIndexOf() and indexOf() This approach checks if the current index is the last occurrence of each element, ensuring we count each distinct element ...

Read More
Showing 2861–2870 of 8,392 articles
« Prev 1 285 286 287 288 289 840 Next »
Advertisements