Nikitasha Shrivastava

Nikitasha Shrivastava

163 Articles Published

Articles by Nikitasha Shrivastava

Page 8 of 17

Remove duplicate items from an array with a custom function in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 506 Views

In JavaScript, arrays often contain duplicate values that need to be removed. This article demonstrates how to create a custom function to eliminate duplicates and return an array with unique elements only. Understanding the Problem We need to create a function that accepts an array as input and returns a new array containing only unique items, excluding any duplicates. For example, given the array [0, 0, 2, 2, 3, 3], the function should return [0, 2, 3]. Algorithm Overview The algorithm iterates through the input array and adds items to a result array only if they ...

Read More

JavaScript Sum odd indexed and even indexed elements separately and return their absolute difference

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 1K+ Views

For a given list of elements, write a JavaScript program to find the sum of its odd and even indexed elements and then, calculate difference between them. To solve this problem, we will first separate odd indexed and even indexed elements. After separating them find their sum separately and store it in different variables. Now, we will calculate the difference between these sums to get desired result. Example Scenario: Input: list = [11, 21, 31, 41, 51, 61]; Output: difference = 30 Here, odd-indexed items are [21, 41, 61] and their sum is ...

Read More

Group a sorted array based on the difference between current and previous elements in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 539 Views

In JavaScript, grouping a sorted array based on the difference between consecutive elements is a common problem where we need to create separate groups whenever the difference exceeds a certain threshold (typically 1). This technique is useful for analyzing sequences and identifying continuous ranges in data. Understanding the Problem Given a sorted array, we need to group elements where consecutive elements have a difference of 1 or less. When the difference between current and previous elements exceeds 1, we start a new group. For example, the array [1, 2, ...

Read More

Shortest distance between objects in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 651 Views

In JavaScript, calculating the shortest distance between two objects requires their coordinates and the Euclidean distance formula. This is commonly used in game development, mapping applications, and geometric calculations. Understanding the Problem To find the shortest distance between two objects, we need their position coordinates (x, y). The distance is calculated using the Euclidean distance formula: √[(x₂-x₁)² + (y₂-y₁)²] The Distance Formula The Euclidean distance formula calculates the straight-line distance between two points in a coordinate system: ...

Read More

How to reverse a string using only one variable in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 308 Views

In this problem statement, our target is to print the reverse string using only one variable and implement the solution with the help of JavaScript. We can solve this problem with the help of loops in JavaScript. Understanding the Problem The given problem is stating that we have a string which we need to reverse using only one variable. In simple terms, if we have the string "Hello World", the reverse of this string will be "dlroW olleH". Logic for the Given Problem In order to reverse the given string with only one variable, we will ...

Read More

Sorting according to weights of numbers in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 1K+ Views

In this problem, we need to sort an array of numbers according to their weights using JavaScript. The weight of a number is defined as the sum of its digits, and we'll create functions to calculate weights and perform the sorting. Understanding the Problem We have to sort numbers according to their weights, where a weight is the sum of a number's digits. For example, if we have an array [19, 11, 12, 15]: Weight of 19 = 1 + ...

Read More

Validating a power JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 247 Views

In JavaScript, validating whether a number is a power of another number (like 3) is a common programming problem. We need to check if a given number can be expressed as nk where n is our base and k is a non-negative integer. To solve this validation, we use arithmetic operators like modulus (%), division (/), and comparison operators. The key insight is that powers of a number can only be divided by that base number repeatedly until we reach 1. Understanding the Problem Let's look at some examples to understand when a number is a power ...

Read More

Function for counting frequency of space separated elements in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 343 Views

In this problem statement, our target is to create a function for counting frequency of space separated elements with the help of JavaScript functionalities. Understanding the Problem The given problem is stating that we have given a space separated elements and our task is to count the frequency of space separated items. So we will provide some strings with some space separated items and assign a result array to see the counts of the frequency of such items. For example if we have a string with space separated items like "Car Bike Car Bicycle Bike", in this example ...

Read More

Merging sorted arrays together JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 973 Views

Merging two sorted arrays in JavaScript involves combining them into a single sorted array. This is a common problem in data structures and algorithms that uses the greedy approach to efficiently merge pre-sorted arrays. What are Sorted Arrays? Sorted arrays are arrays where elements are arranged in a specific order, typically ascending. In JavaScript, arrays are resizable collections that can be sorted using the built-in sort() method. // Array before sorting var arr = [2, 5, 1, 3, 6, 9, 7]; console.log("Before sorting:", arr); ...

Read More

Adding binary strings together JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 3K+ Views

The problem is stating that we have to add two binary strings. Binary strings are a sequence of bytes. To add them in JavaScript, we will first convert them to decimal and calculate the sum. After adding those decimal numbers, we will convert it again into binary strings and print the output. What are Binary Strings? Binary numbers or binary strings are sequences of bytes in base 2. It is basically a combination of 0 and 1 to represent any number. Binary addition is one of the fundamental operations in computer science and works similarly to decimal addition, ...

Read More
Showing 71–80 of 163 articles
« Prev 1 6 7 8 9 10 17 Next »
Advertisements