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 299 of 840
Sorting array of strings having year and month in JavaScript
Suppose, we have an array of strings that contains month-year combined strings like this: const arr = ["2009-feb", "2009-jan", "2010-mar", "2010-jan", "2011-jul", "2011-sep", "2011-jan", "2012-jan", "2012-dec", "2012-feb", "2013-may", "2013-jul", "2013-jun", "2014-jan", "2014-dec", "2014-may", "2015-may", "2015-jan", "2015-jun", "2016-jan", "2016-dec"]; We need to write a JavaScript function that takes such an array and sorts these dates from oldest to latest order. Approach The solution involves creating a custom sorting function that: Splits each date string to separate year and month Converts month names to numeric values ...
Read MoreFinding missing element in an array of numbers in JavaScript
We are required to write a JavaScript function that takes in an array of numbers of length, say n. The array contains all the integers from 0 to n (including both 0 and n), but just one integer is missing, it can be any number and the array is not sorted. The task of our function is to find the missing number and return it in linear time and constant space. Since the array contains all the numbers from 0 to n but one, we can simply calculate the sum of all the elements of the array in linear ...
Read MoreFinding intersection of arrays of intervals in JavaScript
JavaScript function that takes in two arrays, arr1 and arr2 of intervals which are pairwise disjoint and in sorted order. A closed interval [a, b] (with a
Read MoreReturning only odd number from array in JavaScript
JavaScript arrays often contain mixed data types, and finding specific patterns like the one odd number among evens (or vice versa) is a common programming challenge. This article demonstrates how to identify and return the single different element from an array. Problem Statement We need to write a JavaScript function that takes an array of integers as input. The array contains either all even numbers with one odd number, or all odd numbers with one even number. Our function should return this single different element. For example, if the input array is: const arr = ...
Read MoreNon-composite numbers sum in an array in JavaScript
In JavaScript, you can calculate the sum of all prime numbers in an array by first checking each number for primality, then adding the prime numbers together. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Understanding Prime Numbers Prime numbers are numbers like 2, 3, 5, 7, 11, 13, etc. The number 1 is not considered prime, and 2 is the only even prime number. Creating a Prime Check Function First, we need a helper function to determine if a number is prime: ...
Read MoreCompute cartesian product of elements in an array in JavaScript
The Cartesian product of two sets (arrays) A and B, denoted A × B, is the set (array) of all ordered pairs (a, b) where a is in A and b is in B. In simpler terms, a cartesian product of two arrays is a permutation of all possible arrays of two elements whose first element belongs to the first array and the second element belongs to the second array. Example of Cartesian Product If the two arrays are: const arr1 = [1, 2, 3]; const arr2 = [4, 5]; Then their cartesian ...
Read MoreCompare Strings in JavaScript and return percentage of likeliness
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 MoreSimilar string groups in JavaScript
Two strings are similar if we can swap exactly two characters at different positions to make them equal, or if they are already equal. Given an array of strings (all anagrams of each other), we need to find how many groups of similar strings exist. For example, "tars" and "rats" are similar (swap positions 0 and 2), and "rats" and "arts" are similar. This forms one group: {"tars", "rats", "arts"}. The string "star" forms its own group since it's not similar to any other string. Understanding Similarity Two strings are similar if: They are identical, or ...
Read MoreFinding squares in sorted order in JavaScript
We are required to write a JavaScript function that takes in an array of integers, arr, sorted in increasing order. Our function is supposed to return an array of the squares of each number, also sorted in increasing order. For example, if the input to the function is − const arr = [-2, -1, 1, 3, 6, 8]; Then the output should be − const output = [1, 1, 4, 9, 36, 64]; The Challenge Simply squaring and sorting would work, but it's inefficient. Since the original array is ...
Read MoreConverting alphabets to Greek letters in JavaScript
Converting alphabets to Greek letters in JavaScript requires creating a mapping between English and Greek characters, then replacing each character accordingly. Character Mapping First, let's establish the mapping between English and Greek letters: A=α (Alpha) B=β (Beta) D=δ (Delta) E=ε (Epsilon) I=ι (Iota) K=κ (Kappa) N=η (Eta) O=θ (Theta) P=ρ (Rho) R=π (Pi) T=τ (Tau) U=μ (Mu) V=υ (Upsilon) W=ω ...
Read More