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 Nikitasha Shrivastava
Page 5 of 17
Common Character Count in Strings in JavaScript
In JavaScript, finding common characters between two strings involves counting how many characters appear in both strings. This is useful for text analysis, similarity comparisons, and string manipulation tasks. Understanding the Problem The goal is to count common characters between two strings, considering frequency. For example, if we have "abaac" and "baaaa", the common characters are 'a' (appears 3 times in first string, 4 times in second, so we count 3) and 'b' (appears 1 time in both, so we count 1), giving us a total of 4 common characters. Algorithm Step 1: Create a function ...
Read MoreGet key from value in JavaScript
In JavaScript, finding a key from its corresponding value in an object requires iterating through the object's key-value pairs. Unlike accessing values by keys (which is direct), reverse lookup needs a search operation. Understanding the Problem Given a JavaScript object and a value, we need to find the key that maps to that value. For example: { key1: 'value1', key2: 'value2', key3: 'value3' } If we search for 'value2', we should get 'key2' as the result. Using Object.entries() and find() The most efficient ...
Read MoreComparing integers by taking two numbers in JavaScript
Comparing integers is a fundamental operation in JavaScript programming. This tutorial demonstrates how to compare two integers and determine their relationship using JavaScript's comparison operators. Understanding the Problem Integer comparison involves determining the relationship between two numbers - whether one is greater than, less than, or equal to another. This operation is essential for sorting algorithms, conditional logic, and data validation. For example, comparing 15 and 10 tells us that 15 is greater than 10. Comparison Operators in JavaScript ...
Read MoreQueue Reconstruction by Height in JavaScript
The Queue Reconstruction by Height problem involves arranging people in a queue based on their height and the number of taller people in front of them. Each person is represented as [h, k] where h is height and k is the count of people in front with height ≥ h. Understanding the Problem Given an array of people where each person is represented by [height, k], we need to reconstruct the queue. The key insight is that taller people don't affect the positioning of shorter people, so we can process people from tallest to shortest. For example, ...
Read MoreCheck if string ends with desired character in JavaScript
In JavaScript, you can check if a string ends with a specific character using several built-in methods. The most common approaches are using endsWith(), charAt(), or bracket notation. Understanding the Problem We need to determine if a given string ends with a specific character. For example, checking if "Hello World!" ends with "!" should return true, while checking if "Hello World." ends with "!" should return false. Using endsWith() Method (Recommended) The endsWith() method is the most straightforward approach: const str1 = "Hello, Tutorials Point!"; const str2 = "Hello, World."; const desiredChar = "!"; ...
Read MoreRemove number from array and shift the remaining ones JavaScript
In this problem statement, our task is to write a function to remove numbers from an array and shift the remaining elements using JavaScript. We'll explore multiple approaches including the filter method, for loops, and in-place modification to achieve this functionality. Understanding the Problem We need to create a function that removes all occurrences of a given number from an array and shifts the remaining items to fill the gaps. For example, given array [1, 2, 3, 4, 2, 5] and number 2, the result would be [1, 3, 4, 5] with all instances of 2 removed and ...
Read MorePair whose sum exists in the array in JavaScript
In this article we will see how to find pairs of items in an array whose sum equals another element present in the same array. We'll use JavaScript to implement an efficient algorithm for this problem. Understanding the Problem The problem is to find pairs of numbers (a, b) in an array where a + b = c, and c is also present in the array. For example, in array [1, 2, 3, 4, 5], the pair (1, 2) has sum 3, which exists in the array. Approach Using Hash Set We'll use a Set data ...
Read MoreReversing array without changing the position of certain elements JavaScript
In this problem, we need to reverse an array while keeping certain elements in their original positions. This requires tracking preserved element positions and carefully swapping only the non-preserved elements. Understanding the Problem The goal is to reverse an array but preserve specific elements at their original positions. For example, if we have array [1, 2, 3, 4, 5, 6] and want to preserve elements [2, 4, 6], the result should be [5, 2, 3, 4, 1, 6] instead of a complete reversal. Algorithm Approach The solution involves: Track indices of elements to preserve Create ...
Read MoreFactorize a number in JavaScript
In JavaScript, factorizing a number means finding all the prime factors that multiply together to form the original number. This is a fundamental mathematical operation used in cryptography, number theory, and various algorithms. Understanding Prime Factorization Prime factorization breaks down a number into its basic building blocks - prime numbers. For example, 36 = 2 × 2 × 3 × 3, where 2 and 3 are prime factors. We need to find all prime numbers that divide the given number without leaving a remainder. Algorithm Approach The efficient approach iterates from 2 to the square root ...
Read MoreSplit a range of number to a specific number of intervals JavaScript
In this problem statement, our task is to write a function that splits a range of numbers into a specific number of intervals using JavaScript. We need to provide the start value, end value, and the number of intervals. Understanding the Problem The problem requires creating a function that divides a numerical range into equal intervals. The inputs are the starting number, ending number, and desired number of intervals. The output should be an array of subarrays, where each subarray represents an interval with its start and end values. For example, if the range is from 0 ...
Read More