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 371 of 840
Implementing a Binary Search Tree in JavaScript
A tree is a collection of nodes connected by edges, where each node holds data and references to its children. Binary Search Trees (BST) are a special type of binary tree that maintains a sorted order. What is a Binary Search Tree? A Binary Search Tree is a binary tree where nodes with lesser values are stored on the left, and nodes with higher values are stored on the right. This property makes searching, insertion, and deletion operations efficient. 25 ...
Read MoreSubarrays product sum in JavaScript
We are required to write a JavaScript function that takes in an array of numbers of length N such that N is a positive even integer and divides the array into two sub arrays (say, left and right) containing N/2 elements each. The function should calculate the product of each subarray and then add both the results together. Example If the input array is: const arr = [1, 2, 3, 4, 5, 6] The calculation would be: (1*2*3) + (4*5*6) 6 + 120 126 Using Array.reduce() Method Here's ...
Read MoreArray flattening using loops and recursion in JavaScript
Array flattening converts nested arrays into a single-level array. JavaScript provides multiple approaches including loops, recursion, and built-in methods. Problem Overview Given a nested array with mixed data types including falsy values, we need to flatten it completely: const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], [6]]; console.log("Input:", arr); Input: [ [ 1, 2, 3 ], [ 4, 5, [ 5, false, 6, [Array] ] ], [ 6 ] ] Expected output should be a flat array preserving all values including false and null: ...
Read MoreRemove the duplicate value from array with images data in JavaScript
When working with arrays of objects containing image data, you often need to remove duplicates based on a specific property. Here's how to remove duplicate objects based on the 'image' property value. Sample Data Consider an array of image objects where some images appear multiple times: const arr = [{ 'image': "jv2bcutaxrms4i_img.png", 'gallery_image': true }, { 'image': "abs.png", 'gallery_image': true }, { 'image': "acd.png", 'gallery_image': false }, { ...
Read MoreDetermining rank on basis of marks in JavaScript
We are required to write a JavaScript function that takes in an array of numbers representing student marks and returns an array of ranks based on their performance. The function should assign rank 1 to the highest marks, rank 2 to the second highest, and so on. Each student's rank corresponds to their position when marks are sorted in descending order. Problem Statement Given an array of marks, we need to determine the rank of each student based on how their marks compare to others in the class. For example, if the input is: ...
Read MoreMoving vowels and consonants using JavaScript
We are required to write a JavaScript function that takes in a string of English alphabets. Our function should construct a new string where every consonant is pushed forward 9 places through the alphabet, and every vowel is pushed backward 5 places. If they pass 'z', start again at 'a', and if they go before 'a', wrap around to 'z'. Problem Breakdown The transformation rules are: Vowels (a, e, i, o, u): Move backward 5 positions Consonants: Move forward 9 positions Non-alphabetic characters: Keep unchanged Wrapping: Use modulo arithmetic to handle alphabet boundaries Example ...
Read MorePair of (adjacent) elements of an array whose sum is lowest JavaScript
We are required to write a JavaScript function that takes in an array of numbers. The function should return a subarray of two adjacent elements from the original array whose sum is the least amongst all adjacent pairs of the array. If the length of the array is less than 2, we should return boolean false. Example Input For example, if the input array is: const arr = [41, 44, -12, 13, -23, 1, 5, -4, 2, 2]; Here, the sum of pair [-23, 1] is -22 which is the least for any ...
Read MoreJavaScript group array - find the sets of numbers that can be traveled to using the edges defined
Consider the following input and output arrays: const input = ["0:3", "1:3", "4:5", "5:6", "6:8"]; const output = [ [0, 1, 3], [4, 5, 6, 8] ]; Considering each number as a node in a graph, and each pairing x:y as an edge between nodes x and y, we are required to find the sets of numbers that can be traveled to using the edges defined. In graph theory terms, we need to find the distinct connected components within such a graph. For instance, in the above arrays, there ...
Read MoreNext Greater Element in Circular Array in JavaScript
Circular Array An array in which the next element of the last element is the first element of the array is often termed as circular. This concept allows us to treat arrays as if they wrap around, where after the last index, we continue from the first index. Obviously, there exists no such mechanism to store data like this. Data will still be stored in continuous memory blocks, and circular arrays are more like a logical concept than physical reality. Problem We need to find the next greater element for each element in a circular array. The Next ...
Read MoreTransforming array of numbers to array of alphabets using JavaScript
Problem We need to write a JavaScript function that takes an array of numbers and transforms it into a string with four parts separated by hyphens. Each part creates a 4-character "word" from the first two and last two elements of the array under different conditions. The four parts are: Characters from the original array (first, second, second-last, last) Same as above, after sorting the array in ascending order Same as above, after sorting the array in descending order Same as above, after converting to ASCII characters and sorting alphabetically Example Implementation Here's ...
Read More