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 386 of 840
Object comparison Complexity in JavaScript using comparison operator or JSON.stringlify()?
In JavaScript, comparing objects using comparison operators (== or ===) checks reference equality, not content equality. For content comparison, JSON.stringify() can be used with limitations. The Problem with Comparison Operators When comparing objects with == or ===, JavaScript checks if both variables reference the same object in memory, not if their contents are identical: var object1 = { firstName: "David" }; var object2 = { firstName: "David" }; var object3 = object1; console.log("object1 == object2:", object1 == object2); // false - different objects console.log("object1 === object2:", object1 === object2); // false - different ...
Read MoreReplace multiple instances of text surrounded by specific characters in JavaScript?
Let's say we have a string where certain text is surrounded by special characters like hash (#). We need to replace these placeholders with actual values. var values = "My Name is #yourName# and I got #marks# in JavaScript subject"; We need to replace the special character placeholders with valid values. For this, we use replace() along with shift(). Using replace() with shift() The replace() method with a regular expression can find all instances of text surrounded by hash characters. The shift() method removes and returns the first element from an array, making it ...
Read MoreAre the strings anagrams in JavaScript
Two strings are said to be anagrams of each other if by rearranging, rephrasing or shuffling the letters of one string we can form the other string. Both strings must contain exactly the same characters with the same frequency. For example, 'something' and 'emosghtin' are anagrams of each other because they contain the same letters with the same count. We need to write a JavaScript function that takes two strings and returns true if they are anagrams of each other, false otherwise. Method 1: Character Frequency Count This approach counts the frequency of each character in ...
Read MoreFinding hamming distance in a string in JavaScript
The Hamming distance between two strings of equal length is the number of positions at which the corresponding characters differ. It measures the minimum number of single-character edits needed to transform one string into another. In JavaScript, we can calculate Hamming distance by comparing each character position and counting the differences. This metric is commonly used in coding theory, cryptography, and bioinformatics. Basic Implementation Here's a JavaScript function that calculates the Hamming distance between two strings: const str1 = 'Hello World'; const str2 = 'Heeyy World'; const findHammingDistance = (str1 = '', str2 = ...
Read MoreBinary array to corresponding decimal in JavaScript
Problem We are required to write a JavaScript function that takes in a binary array (consisting of only 0 and 1). Our function should first join all the bits in the array and then return the decimal number corresponding to that binary. Method 1: Using Math.pow() with Manual Calculation This approach calculates the decimal value by iterating through the array and using powers of 2: const arr = [1, 0, 1, 1]; const binaryArrayToNumber = arr => { let num = 0; for (let i = ...
Read MoreFind and return the longest length of set in JavaScript
In JavaScript, finding the longest length of a set involves traversing array elements in a cycle pattern where each element points to the next index. This problem requires tracking visited elements to avoid infinite loops and find the maximum cycle length. Problem Statement Given an array of length N containing all integers from 0 to N-1, we need to find the longest set S where S[i] = {A[i], A[A[i]], A[A[A[i]]], ...}. We start from index i, then move to A[i], then to A[A[i]], and continue until we encounter a duplicate element. Example Input and Output For ...
Read MoreVariable defined with a reserved word const can be changed - JavaScript?
In JavaScript, variables declared with const cannot be reassigned, but this doesn't mean the contents of objects or arrays are immutable. You can still modify the properties of objects declared with const. Understanding const with Objects The const keyword prevents reassignment of the variable itself, but object properties can still be modified: const details = { firstName: 'David', subjectDetails: { subjectName: 'JavaScript' } }; // This works - modifying object properties details.firstName = 'John'; details.subjectDetails.subjectName = 'Python'; console.log(details); { firstName: 'John', subjectDetails: { ...
Read MoreChecking for the similarity of two 2-D arrays in JavaScript
We are required to write a JavaScript function that takes in two 2-D arrays and returns a boolean based on the check whether the arrays are equal or not. The equality of these arrays, in our case, is determined by the equality of corresponding elements. Both the arrays should have same number of rows and columns. Also, arr1[i][j] === arr2[i][j] should yield true for all i between [0, number of rows] and j between [0, number of columns] Method 1: Basic Nested Loop Approach This method compares each element position by position using nested loops: ...
Read MoreSubset with maximum sum in JavaScript
In JavaScript, finding the subset of non-adjacent elements with maximum sum is a classic dynamic programming problem. We need to decide whether to include each element or skip it, ensuring no two adjacent elements are selected. The key insight is that for each element, we have two choices: include it (and skip the previous element) or exclude it (and take the maximum sum up to the previous element). Problem Statement Given an array of integers, find the subset of non-adjacent elements that produces the maximum sum. Adjacent elements cannot be selected together. For example, with the ...
Read MoreGrouping words with their anagrams in JavaScript
Two words or phrases which can be made by arranging the letters of each other in a different order are called anagrams of each other, like "rat" and "tar". We need to write a JavaScript function that takes in an array of strings that might contain some anagram strings. The function should group all the anagrams into separate subarrays and return the new array thus formed. Problem Example If the input array is: const arr = ['rat', 'jar', 'tar', 'raj', 'ram', 'arm', 'mar', 'art']; Then the output array should be: const ...
Read More