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
Javascript Articles
Page 140 of 534
Finding 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 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 MoreAlternative sorting of an array in JavaScript
We need to write a JavaScript function that sorts an array in an alternating pattern where elements follow the sequence: smaller, larger, smaller, larger, and so on. The pattern we want to achieve is: arr[0] < arr[1] > arr[2] < arr[3] > arr[4] < arr[5]... This creates a zigzag or wave-like pattern in the sorted array. There can be multiple valid solutions for any given input array. Example Input and Output For an input array: const arr = [1, 2, 3, 4, 5, 6]; One possible output could be: ...
Read MoreFinding intersection of arrays that contain repetitive entries in JavaScript
Finding the intersection of arrays with repetitive entries means identifying common elements between two arrays while preserving duplicate occurrences. This is different from a simple set intersection, as we need to consider the frequency of each element. The function should build a third array based on the two input arrays that contains all the elements that are common to both arrays. If there are multiple instances of the same element present in both arrays, we need to include all such instances up to the minimum count found in either array. Problem Example If the input arrays are: ...
Read MoreFormatting a string to separate identical characters in JavaScript
In JavaScript, we often need to rearrange string characters so that no two identical characters are adjacent to each other. This is a classic string manipulation problem that requires careful character distribution. The function should reorganize the characters in the input string such that no two identical characters are placed next to each other. If such an arrangement is possible, it returns the rearranged string; otherwise, it returns an empty string. Problem Understanding Given a string like 'add', we need to rearrange it to 'dad' where no two identical characters are adjacent. The key insight is that ...
Read MoreMatching odd even indices with values in JavaScript
We are required to write a JavaScript function that takes in an array of numbers. The array given as an input to the function have two special properties − The length of the array will always be an even number. The number of even numbers and the number of odd numbers in the array will always be equal (i.e., both being equal to the half of the length of array) The function should shuffle the elements of the array such that all the even values occupy even indices ...
Read MoreRemoving smallest subarray to make array sum divisible in JavaScript
We are required to write a JavaScript function that takes in an array of positive integers as the first argument and a positive integer as the second argument. The function should figure out and return the length of the smallest subarray that we should delete from the original array in order to make its sum divisible by the number specified by the second argument. For example, if we have an array [3, 8, 2, 6] and want the sum divisible by 9, we need to remove the subarray [8, 2] of length 2. Problem Analysis The ...
Read MoreFinding the longest Substring with At Least n Repeating Characters in JavaScript
We are required to write a JavaScript function that takes in a string as the first argument and a positive integer n as the second argument. The string is likely to contain some repeating characters. The function should find out and return the length of the longest substring from the original string in which all characters appear at least n number of times. For example, if the input string is 'kdkddj' and n is 2, the output should be 5 because the desired longest substring is 'kdkdd'. Problem Understanding Given a string and a number n, ...
Read MoreSorting array according to increasing frequency of elements in JavaScript
We are required to write a JavaScript function that takes in an array of literals as the first and the only argument. The array is likely to contain many repeating values. Our function should sort the array such that the values that are unique or that have the least frequency are placed before the ones that have the most. For example − If the input array is − const arr = [4, 7, 3, 5, 5, 4, 7, 9, 2, 1, 5, 7, 5, 5, 9]; Then the output array should be − ...
Read MoreArranging words by their length in a sentence in JavaScript
In JavaScript, you can arrange words in a sentence by their length using string manipulation and array sorting methods. This technique is useful for text processing and formatting applications. A sentence is a string of characters joined by whitespaces. The goal is to rearrange words so the shortest word appears first, followed by progressively longer ones. Problem Example If the input string is: const str = 'this is a string'; Then the output should be: const output = 'a is this string'; Solution Using Array Methods Here's a ...
Read More