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 309 of 840
Finding one missing number in a scrambled sequence using JavaScript
We are required to write a JavaScript function that takes in an array of numbers containing numbers from 1 to n. The problem is that one number from the array goes missing and the array is not sorted as well. Our function should find and return that one number missing from the array. Problem Statement Given an array of numbers from 1 to n with one missing number, find the missing number. The array is not sorted and contains n-1 elements instead of n. Using Sum Formula Method The most efficient approach uses the mathematical formula ...
Read MoreCounting smaller and greater in JavaScript
In JavaScript, we often need to count elements in an array that are greater than or smaller than a specific value. This is useful for data analysis, filtering, and statistical operations. Let's say we have an array of numbers and want to count how many elements are greater than and smaller than a given number n. const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9]; console.log("Array:", arr); Array: [3, 5, 5, 2, 23, 4, 7, 8, 8, 9] Using reduce() Method The most elegant approach uses the ...
Read MoreRegroup JSON array in JavaScript
Suppose, we have a JSON array of objects like this − const arr = [ { "id": "03868185", "month_10": 6, }, { "id": "03870584", "month_6": 2, }, { "id": "03870584", ...
Read MoreIntegers have sum of squared divisors as perfect square in JavaScript
Problem We need to write a JavaScript function that takes a range specified by two numbers m and n, and finds all integers between m and n where the sum of their squared divisors is itself a perfect square. The function should return an array of subarrays, where each subarray contains the number and the sum of its squared divisors. Understanding the Concept For a number to qualify, we need to: Find all divisors of the number Square each divisor and sum them up ...
Read MoreSpecial type of sort of array of numbers in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and sorts the array such that first all the even numbers appear in ascending order and then all the odd numbers appear in ascending order. For example: If the input array is − const arr = [2, 5, 2, 6, 7, 1, 8, 9]; Expected Output Then the output should be − [2, 2, 6, 8, 1, 5, 7, 9] Using Custom Comparator Function We can solve this by creating a custom comparator function ...
Read MoreWriting table of number in array in JavaScript
We are required to write a JavaScript function that takes in two numbers, say m and n, and it returns an array of first n multiples of m. Therefore, let's write the code for this function − Example The code for this will be − const num1 = 4; const num2 = 6; const multiples = (num1, num2) => { const res = []; for(let i = num1; i { return Array.from({length: count}, (_, index) => base * (index + 1)); }; console.log(generateTable(7, 5)); console.log(generateTable(3, 8)); [ 7, 14, 21, 28, 35 ] [ 3, 6, 9, 12, 15, 18, 21, 24 ] Using a Simple For Loop Here's another straightforward approach using a basic for loop: function createMultiplicationTable(number, count) { const table = []; for (let i = 1; i
Read MoreFinding the length of the diagonal of a cuboid using JavaScript
We need to write a JavaScript function that calculates the diagonal length of a cuboid (rectangular box) given its length, width, and height dimensions. Problem We are required to write a JavaScript function that takes in the length, width and height of a cuboid and return the length of its diagonal. Formula The space diagonal of a cuboid is calculated using the 3D Pythagorean theorem: diagonal = √(length² + width² + height²) Example Following is the code: const height = 10; const width = 12; const length = 15; ...
Read MoreIs element repeated more than n times in JavaScript
In JavaScript, you may need to check if any element appears more than a specified number of times in an array. This is useful for data validation, duplicate detection, and enforcing constraints on array contents. Problem Definition We need to write a function that takes two arguments: An Array of literals that may contain repeating elements A number representing the maximum allowed occurrences (limit) The function should return false if any element appears more than the limit, and true otherwise. Using reduce() and every() Methods The ...
Read MoreCheck how many objects are in the array with the same key in JavaScript
Suppose, we have an array of objects containing some data about some users like this − const arr = [ { "name":"aaa", "id":"2100", "designation":"developer" }, { "name":"bbb", "id":"8888", "designation":"team lead" }, { "name":"ccc", "id":"6745", ...
Read MoreFinding the balance of brackets in JavaScript
Given a string that consists of only two types of characters: "(" and ")". We are required to write a function that takes in one such string and balances the parentheses by inserting either a "(" or a ")" as many times as necessary. The function should then return the minimum number of insertions made in the string to balance it. For example: If the string is − const str = '()))'; Then the output should be 2, because by prepending '((' we can balance the string. How It Works The algorithm uses ...
Read More