Front End Technology Articles

Page 218 of 652

Find the Length of the Longest possible palindrome string JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 479 Views

Given a string s which consists of lowercase or uppercase letters, we are required to return the length of the longest palindrome that can be built with those letters. Letters are case sensitive, for example, "Aa" is not considered a palindrome here. For example, if the input string is "abccccdd", the output should be 7 because one longest palindrome that can be built is "dccaccd", whose length is 7. Algorithm Explanation The key insight is that a palindrome can have pairs of characters (which appear on both sides) plus at most one character in the middle. We ...

Read More

Finding perfect numbers in JavaScript

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

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly. For example: 28 is a perfect number, because 28 = 1 + 2 + 4 + 7 + 14 We are required to write a JavaScript function that takes in a number, say n, and determines whether or not n is a perfect number. How Perfect Numbers Work To check if a number is perfect, we need to: ...

Read More

Sort in multi-dimensional arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 820 Views

Sorting multi-dimensional arrays in JavaScript requires handling each subarray while maintaining specific sorting rules. This article demonstrates how to sort elements within subarrays based on custom conditions. Problem Statement Suppose we have the following array of arrays: const arr = [ ["A", "F", "A", "H", "F", "F"], ["F", "A", "A", "F", "F", "H"] ]; We need to write a JavaScript function that sorts all subarrays according to these rules: If the elements are not either "A" or "F", they should maintain their position If the element is either ...

Read More

Is uppercase used correctly JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 130 Views

In JavaScript, we can determine whether a string follows correct uppercase usage based on three specific rules. A string is considered to have correct uppercase usage if it meets any of these criteria: All letters in a word are capitals, like "INDIA". All letters in a word are not capitals, like "example". Only the first letter in a word is capital, like "Ramesh". We need to write a JavaScript function that takes a string and determines whether it complies with any of these three ...

Read More

Implementing binary search in JavaScript to return the index if the searched number exist

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 719 Views

We are required to write a JavaScript function that takes in a sorted array of numbers as the first argument and a search number as the second argument. If the search number exists in the array, we need to return its index in the array, otherwise we need to return -1. We have to do this making use of the binary search algorithm. The binary search algorithm is basically a divide and conquer algorithm which recursively divides the array into halves until it converges to a singleton element. The sorting of array is necessary for binary search ...

Read More

Is the second string a rotated version of the first string JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 219 Views

We are required to write a JavaScript function that takes in two strings, say str1 and str2. We are required to determine whether or not the second string is a rotated version of the first string. For example − If the input strings are − const str1 = 'abcde'; const str2 = 'cdeab'; Then the output should be true because str2 is indeed made by shifting 'ab' to the end of string in str1. Understanding String Rotation A string rotation means taking some characters from the beginning of a string and moving them ...

Read More

Merge JavaScript objects with the same key value and count them

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

Suppose we have an array of objects like this: const arr = [{ "value": 10, "id": "111", "name": "BlackCat", }, { "value": 10, "id": "111", "name": "BlackCat", }, { "value": 15, "id": "777", "name": "WhiteCat", }]; We are required to write a JavaScript function that takes in one such array. The function should then merge all those objects together that ...

Read More

Merge JSON array date based JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 817 Views

When working with JSON arrays containing objects with date properties, you often need to merge objects that share the same date. This is common when combining data from different sources or consolidating time-series data. Suppose we have the following array of objects: const arr = [ { "date": "2010-01-01", "price": 30 }, { "date": "2010-02-01", ...

Read More

Sort array according to the date property of the objects JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 228 Views

In JavaScript, sorting an array of objects by date requires converting date strings to Date objects for proper comparison. The Array.sort() method with a custom comparator function handles this efficiently. Sample Data Consider an array of objects with date properties: const arr = [ {date: "2016-06-08 18:10:00"}, {date: "2016-04-26 20:01:00"}, {date: "2017-02-06 14:38:00"}, {date: "2017-01-18 17:30:21"}, {date: "2017-01-18 17:24:00"} ]; Using Array.sort() with Date Comparison The most straightforward approach uses Array.sort() with a ...

Read More

Dynamic Programming: Is second string subsequence of first JavaScript

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

We are given two strings str1 and str2, we are required to write a function that checks if str1 is a subsequence of str2. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. For example, "ace" is a subsequence of "abcde" while "aec" is not because the relative order is not maintained. Understanding Subsequences In a subsequence, characters must appear in the same order as in the original string, but they ...

Read More
Showing 2171–2180 of 6,519 articles
« Prev 1 216 217 218 219 220 652 Next »
Advertisements