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 306 of 840
Sorting array of exactly three unique repeating elements in JavaScript
Suppose we have an array of Numbers that contains any frequency of exactly three elements: -1, 0 and 1 like this: const arr = [1, 1, 0, -1, 1, 0, -1, 1, 0, 0, 1]; console.log("Original array:", arr); Original array: [ 1, 1, 0, -1, 1, 0, -1, 1, 0, 0, 1 ] We are required to write a JavaScript function that takes in one such array. The function should simply sort this special array in place (without using any extra array to store the values). The only condition is that our function ...
Read MoreChecking for squared similarly of arrays in JavaScript
We are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2, as the first and second argument respectively. Our function should return true if and only if every element in arr2 is the square of any element of arr1 irrespective of their order of appearance. For example, if the input to the function is: Input const arr1 = [4, 1, 8, 5, 9]; const arr2 = [81, 1, 25, 16, 64]; Output const output = true; Using Frequency Map Approach The ...
Read MoreMapping string to Numerals in JavaScript
We are required to write a JavaScript function that takes in a string. It should print out each number for every corresponding letter in the string. Letter to Number Mapping Each letter corresponds to its position in the alphabet: a = 1 b = 2 c = 3 d = 4 e = 5 ... y = 25 z = 26 Note: The function should remove any special characters and spaces, processing only alphabetic characters. Example Input and Output If the input is: "hello man" Then the output ...
Read MoreInserting a new interval in a sorted array of intervals in JavaScript
For the purpose of this question, we define an interval as an array of two numbers where the first number is always smaller than the second number. For example − [4, 6], [2, 3], [6, 8], [2, 7], [1, 8] are all examples of valid intervals. Suppose, we have an array of intervals which is sorted according to their start times (the first elements of each interval). The intervals in the array are non-overlapping which means that for any two arbitrary adjacent intervals: [m, n], [x, y] m < n < x ...
Read MoreValidating alternating vowels and consonants in JavaScript
Validating alternating vowels and consonants is a common string pattern validation problem. We need to check if vowels (a, e, i, o, u) and consonants alternate throughout the string. Problem Statement Write a JavaScript function that takes a string of English alphabets and returns true if vowels and consonants appear alternatingly, false otherwise. For example: Input: 'amazon' Output: true Explanation: a(vowel) → m(consonant) → a(vowel) → z(consonant) → o(vowel) → n(consonant) Solution const str = 'amazon'; const appearAlternatingly = (str = '') => { if (str.length === 0) ...
Read MoreConstructing largest number from an array in JavaScript
We need to write a JavaScript function that takes an array of numbers and arranges them to form the largest possible number by concatenating the digits. The key insight is that we can't simply sort numbers in descending order. For example, with numbers [3, 30], sorting gives [30, 3] → "303", but the correct answer is [3, 30] → "330". For example − If the input array is − const arr = [5, 45, 34, 9, 3]; Then the output should be − '954543' Algorithm The solution uses ...
Read MoreNext multiple of 5 and binary concatenation in JavaScript
We need to write a JavaScript function that takes a number and finds the next higher multiple of 5 by concatenating the shortest possible binary string to the number's binary representation. Problem Given a number n, we want to: Convert n to binary representation Append the shortest binary string to make it divisible by 5 Return the resulting decimal number How It Works The algorithm generates all possible binary combinations of increasing length until it finds one that, when concatenated to the original number's binary form, creates a number divisible by 5. Example ...
Read MoreFinding special kind of elements with in an array in JavaScript
This article explains how to find special pairs of elements in an array where both the value difference and index difference meet specific criteria. Problem Statement We need to write a JavaScript function that takes three arguments: arr --> an array of integers m --> maximum allowed index difference n --> maximum allowed value difference The function should find if there exist two elements (a1 and a2) such that: The absolute difference between a1 and a2 is at most n The absolute difference between the indices of a1 and a2 is at ...
Read MoreHow to Access element from Table using JavaScript?
To access table elements using JavaScript, you can use methods like document.getElementById() or document.getElementsByTagName() to select the table, then access individual rows and cells. This article demonstrates how to access table rows and create an interactive hover effect. Basic Table Access Methods JavaScript provides several ways to access table elements: By ID: document.getElementById('tableId') - Access a specific table By Tag Name: document.getElementsByTagName('tr') - Access all table rows Table Properties: Use table.rows or table.childNodes to access rows Example: Interactive Table with Hover Effect Let's create a table where rows highlight when you hover over ...
Read MoreCalculating h index of a citation in JavaScript
The h-index is a metric used to measure both the productivity and citation impact of a researcher. In JavaScript, we can calculate it by analyzing an array of citation counts for a researcher's papers. What is H-Index? A researcher has h-index of value h if they have h papers with at least h citations each, and the remaining papers have no more than h citations each. For example, if a researcher has papers with citations [1, 6, 3, 0, 5], they have 3 papers with at least 3 citations (papers with 6, 3, and 5 citations), so the ...
Read More