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 Nikitasha Shrivastava
Page 8 of 17
Remove duplicate items from an array with a custom function in JavaScript
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 MoreJavaScript Sum odd indexed and even indexed elements separately and return their absolute difference
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 MoreGroup a sorted array based on the difference between current and previous elements in JavaScript
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 MoreShortest distance between objects in JavaScript
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 MoreHow to reverse a string using only one variable in JavaScript
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 MoreSorting according to weights of numbers in JavaScript
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 MoreValidating a power JavaScript
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 MoreFunction for counting frequency of space separated elements in JavaScript
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 MoreMerging sorted arrays together JavaScript
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 MoreAdding binary strings together JavaScript
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