AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 301 of 840

Finding points nearest to origin in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 715 Views

Problem We need to write a JavaScript function that takes an array of coordinates and a number as arguments. The function should find and return the specified number of closest points to the origin (0, 0) using Euclidean distance. The Euclidean distance between a point (x, y) and the origin is calculated as: √(x² + y²) Example Input and Output For the input: const arr = [[3, 3], [5, -1], [-2, 4]]; const num = 2; The expected output should be: [[3, 3], [-2, 4]] Solution Using Sort ...

Read More

Finding length of repeating decimal part in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 473 Views

When dividing 1 by certain numbers, the decimal result may have a repeating pattern. This function finds the length of that repeating decimal part, but only for numbers that are coprime with 10 (share no common factors except 1). Problem Statement We need to write a JavaScript function that: Checks if a number is coprime with 10 (shares no common factors except 1) If not coprime, returns -1 If coprime, returns the length of the repeating decimal part when 1 is divided by that number ...

Read More

Rounding off numbers to some nearest power in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 226 Views

We are required to write a JavaScript function that takes in a number and returns a number that can be represented as a power of 2 which is nearest to the input number. For example: If the input number is 145, the output should be 128 because 128 (which is 2^7) is the nearest power of 2 to 145. Understanding Powers of 2 Powers of 2 are numbers like: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512... Each number is double the previous one. Algorithm Approach The algorithm works by: ...

Read More

Merging two arrays in a unique way in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 185 Views

We are required to write a JavaScript function that takes in two arrays and merges the arrays taking elements alternatively from the arrays. For example If the two arrays are − const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3]; Then the output should be − [4, 2, 3, 1, 2, 6, 5, 8, 6, 9, 8, 4, 9, 3] Using Index-Based Approach The code for this will be − const arr1 = [4, 3, 2, ...

Read More

How to count the occurrence of a specific string in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 284 Views

In JavaScript, counting occurrences of a substring within a string is a common task. There are several approaches to accomplish this, from simple loops to built-in string methods. For example, counting how many times "is" appears in "this is a string" should return 2. count('this is a string', 'is') should return 2; Method 1: Using indexOf() with Loop The most reliable approach uses indexOf() to find each occurrence and increment a counter: const str1 = 'this is a string'; const str2 = 'is'; function countOccurrences(mainStr, subStr) { ...

Read More

Pair of similar elements at different indices in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 146 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument. The function is required to count the number of all such element pairs from the array that are equal in magnitude but are present at different indices. For example, if the input array is: const arr = [7, 9, 5, 7, 7, 5]; Then the output should be: 4 because the desired pairs are [7, 7], [7, 7], [7, 7], [5, 5] How It Works The ...

Read More

Checking for univalued Binary Search Tree in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 192 Views

A binary search tree is univalued if every node in the tree has the same value. This means all nodes must contain identical data values for the tree to be considered univalued. Problem We need to write a JavaScript function that takes the root of a BST and returns true if the tree is univalued, false otherwise. For example, if the nodes of the tree are: const input = [5, 5, 5, 3, 5, 6]; Then the output should be: const output = false; Binary Search Tree Implementation ...

Read More

Sorting one string by the order of second in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 278 Views

We are required to write a JavaScript function that takes in two strings, str1 and str2 as the first and the second argument. Our function should sort str1 according to the order of characters as they appear in str2. Characters that appear first in str2 are placed first, followed by ones that come later, and finally followed by letters absent in str2. Problem Example For example, if the input to the function is: Input const str1 = 'coding'; const str2 = 'gncabdi'; Expected Output gncdio Output Explanation Looking at str2 ...

Read More

Adding up identical elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 155 Views

We are required to write a JavaScript function that takes in an array of numbers and sums all the identical numbers together at one index. Problem Overview When we have an array with duplicate values, we want to combine them into a single occurrence with their sum. The first occurrence of each unique number will contain the total sum of all occurrences. Example Input and Output If the input array is: const arr = [20, 10, 15, 20, 15, 10]; Then the output should be: const output = [40, 20, ...

Read More

Iterating through an array, adding occurrences of a true in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 186 Views

Suppose we have an array of true/false values represented by 't'/'f' which we retrieved from some database like this − const arr = ['f', 't', 'f', 't', 't', 't', 'f', 'f', 't', 't', 't', 't', 't', 't', 'f', 't']; console.log(arr); [ 'f', 't', 'f', 't', 't', 't', 'f', 'f', 't', 't', 't', 't', 't', 't', 'f', 't' ] We need to count consecutive occurrences of 't' that are sandwiched between two 'f's and return an array of those counts. Array: ['f', 't', 'f', 't', 't', 't', 'f', 'f', 't', ...

Read More
Showing 3001–3010 of 8,392 articles
« Prev 1 299 300 301 302 303 840 Next »
Advertisements