Object Oriented Programming Articles

Page 54 of 589

Longest decreasing subsequence subarray in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 265 Views

We are required to write a JavaScript function that takes in an array of integers and returns the length of the longest consecutive decreasing subsequence (subarray) from the array. Problem Statement Given an array of integers, find the length of the longest contiguous subsequence where each element is smaller than the previous one. For example, if the input array is: const arr = [5, 2, 5, 4, 3, 2, 4, 6, 7]; The longest decreasing subsequence is [5, 4, 3, 2] with length 4. Solution Approach We'll track the current decreasing ...

Read More

Fuzzy Search Algorithm in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Fuzzy search allows finding strings that match a pattern even when characters are not consecutive. In JavaScript, we can implement a fuzzy search algorithm that checks if characters from a search query appear in the same order within a target string. How Fuzzy Search Works The algorithm loops through each character of the search query and verifies that all characters exist in the target string in the same sequential order, though they don't need to be adjacent. For example: ('a haystack with a needle').fuzzySearch('hay sucks'); // false ('a haystack with a needle').fuzzySearch('sack hand'); // true ...

Read More

Modify an array based on another array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 998 Views

When working with arrays in JavaScript, you might need to modify one array based on patterns or elements found in another array. This article demonstrates how to join consecutive elements from a reference array when they appear as combined phrases in a second array. Problem Statement Suppose we have a reference array of individual words: const reference = ["your", "majesty", "they", "are", "ready"]; And we want to modify it based on another array that contains some words joined together: const another = ["your", "they are"]; The goal is to create ...

Read More

Calculating the sum of digits of factorial JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 544 Views

We are required to write a JavaScript function that takes in a number. The function should first calculate the factorial of that number and then it should return the sum of the digits of the calculated factorial. For example, for the number 6, the factorial will be 720, so the sum of digits (7 + 2 + 0) should be 9. Understanding the Problem This problem involves two steps: Calculate the factorial of a given number Sum all digits in the factorial result Step 1: Calculate ...

Read More

Insert a number into a sorted array of numbers JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

We are required to write a JavaScript function that takes in a sorted array of numbers as the first argument and a single number as the second argument. The function should push the number specified as the second argument into the array without distorting the sorting of the elements. We are required to do this without creating another array. Approach: Binary Search with In-Place Insertion The solution uses binary search to find the correct insertion position, then shifts elements using a swapping technique to maintain order without extra space. Example const arr = ...

Read More

Check if a string is entirely made of the same substring JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 419 Views

In JavaScript, you can check if a string is entirely made of repeated substrings using various approaches. This is useful for pattern validation and string analysis. The problem requires that the string consists of a repeated character sequence with at least one repetition. For example, "aa" contains two "a" substrings, "abcabcabc" contains three "abc" substrings, but "ababa" fails because it has an extra character. Examples of Valid and Invalid Patterns "aa" should return true because it entirely contains two strings "a" "aaa" should return true because it entirely ...

Read More

Compare Strings in JavaScript and return percentage of likeliness

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

We are required to write a JavaScript function that can compare two strings and return the percentage likeliness of how much they are alike. The percentage will be nothing but a measure of many characters the two strings have in common. If they are completely similar the output should be 100, and if they contain no common character at all, the output should be 0. Understanding the Algorithm This implementation uses the Levenshtein distance algorithm to calculate string similarity. The Levenshtein distance measures the minimum number of single-character edits (insertions, deletions, or substitutions) needed to change one ...

Read More

Sorting array of Number by increasing frequency JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

We are required to write a JavaScript function that takes in an array of numbers that might contain some repeating numbers. The function should sort the array such that the elements that are repeated for the least number of times appears first followed by the elements with increasing frequency. For example − If the input array is − const arr = [1, 1, 2, 2, 2, 3]; Then the sorted array should be − const output = [3, 1, 1, 2, 2, 2]; How It Works The solution ...

Read More

JavaScript - find distance between items on array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 377 Views

In JavaScript, finding the distance between items in an array involves calculating the difference between each element and all succeeding elements. This creates a matrix of distances that can be useful for various algorithms and data analysis. Suppose we have a sorted (increasing order) array of Numbers like this: const arr = [2, 5, 7, 8, 9]; We need to write a JavaScript function that takes in one such array. The function should construct a new subarray for each element of the input array containing the differences between that element and all succeeding elements. ...

Read More

Finding Number of Days Between Two Dates JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 325 Views

Finding the number of days between two dates is a common requirement in JavaScript applications. While you can implement complex date calculations manually, JavaScript's built-in Date object provides a much simpler and more reliable approach. The Simple Approach Using Date Objects The most straightforward method is to convert date strings to Date objects and calculate the difference in milliseconds, then convert to days: const daysBetweenDates = (date1, date2) => { const firstDate = new Date(date1); const secondDate = new Date(date2); ...

Read More
Showing 531–540 of 5,881 articles
« Prev 1 52 53 54 55 56 589 Next »
Advertisements