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
Web Development Articles
Page 239 of 801
Finding 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 MoreSmallest prime number just greater than the specified number in JavaScript
We are required to write a JavaScript function that takes in a positive integer as the first and the only argument. The function should find one such smallest prime number which is just greater than the number specified as argument. For example − If the input is − const num = 18; Then the output should be: const output = 19; Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. To check if a ...
Read MoreFinding the smallest positive integer not present in an array in JavaScript
We are required to write a JavaScript function that takes in array of integers as the first and the only argument. Our function should find and return that smallest positive integer which is not present in the array. For example, if the input array is: const arr = [4, 2, -1, 0, 3, 9, 1, -5]; Then the output should be: 5 Because 1, 2, 3, 4 are already present in the array and 5 is the smallest positive integer absent from the array. Using Sequential Search Approach ...
Read MoreSearching in a sorted 2-D array in JavaScript
We are required to write a JavaScript function that takes in an array of arrays of numbers as the first argument and a number as the second argument. The subarrays contain numbers sorted in an increasing order and no element of a preceding subarray is greater than any element of the succeeding subarray. The function should use the binary search algorithm to search for the element provided as the second argument in the sorted array of arrays. If the element exists the function should return true, false otherwise. For example − If the input array is ...
Read MoreFinding words in a matrix in JavaScript
We need to write a JavaScript function that takes a 2D character matrix and a string, then determines if the string can be formed by connecting adjacent characters in the matrix without reusing any position. The function searches for a path through the matrix where each character in the target string appears in sequence through adjacent cells (up, down, left, right). Each cell can only be used once per word search. Problem Example Given this matrix and target string: const arr = [ ['s', 'd', 'k', 'e'], ...
Read MoreFinding all possible subsets of an array in JavaScript
Finding all possible subsets of an array is a common algorithmic problem. A subset is any combination of elements from the original array, including the empty set and the full array itself. For an array of n elements, there are 2^n possible subsets. This is because each element can either be included or excluded from a subset. For example, if the input array is: const arr = [1, 2, 3]; The output should contain all possible subsets: [ [], [1], [2], [3], ...
Read MoreFinding smallest element in a sorted array which is rotated in JavaScript
We are required to write a JavaScript function that takes in an array of integers as the only argument. The array is first sorted and then rotated by any arbitrary number of elements. Our function should find the smallest element in the array and return that element. The only condition is that we have to do this in less than linear time complexity, using a modified binary search algorithm that achieves O(log n) time complexity. Problem Understanding In a rotated sorted array like [6, 8, 12, 25, 2, 4, 5], the original sorted array [2, 4, ...
Read MoreFinding the length of second last word in a sentence in JavaScript
A sentence is just a string which contains strings (called words) joined by whitespaces. We are required to write a JavaScript function that takes in one such sentence string and count the number of characters in the second to last word of the string. If the string contains no more than 2 words, our function should return 0. For example − If the input string is − const str = 'this is an example string'; Then the output should be − const output = 7; because the number of characters ...
Read More