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
Javascript Articles
Page 248 of 534
Finding the only unique string in an array using JavaScript
In JavaScript, finding a unique string that appears only once in an array is a common programming challenge. This article demonstrates two effective approaches to solve this problem. Problem Statement Write a JavaScript function that takes an array of strings as input and returns the only unique string present in the array. A unique string is defined as a string that occurs only once in the given array. If there are no unique strings, the function should return null. Sample Input: const strings = ["apple", "banana", "orange", "banana", "kiwi", "kiwi", "apple"]; Sample Output: ...
Read MoreRemoving all spaces from a string using JavaScript
JavaScript provides several efficient methods to remove all spaces from a string. This is a common requirement in web development for tasks like data validation, formatting user input, or preparing strings for processing. Problem Statement Given a string containing spaces, we need to remove all space characters to create a continuous string without any gaps. Sample Input: "Hello world! This is a test string." Sample Output: "Helloworld!Thisisateststring." Methods to Remove Spaces We'll explore four different approaches to solve this problem: Using Regular Expression with ...
Read MoreRemoving punctuations from a string using JavaScript
Removing punctuation from strings is a common task in text processing applications. JavaScript provides several effective methods to accomplish this, from regular expressions to iterative approaches. Problem Statement Create a JavaScript function that removes all punctuation marks from a given string while preserving letters, numbers, and spaces. Sample Input: Hello, world! How's everything going? Sample Output: Hello world Hows everything going Using Regular Expression (Recommended) Regular expressions provide the most efficient way to remove punctuation. The pattern /[^\w\s]/g matches any character that isn't a word character (letters, digits) or whitespace. ...
Read MoreSorting numbers in ascending order and strings in alphabetical order in an array in JavaScript
In JavaScript, sorting arrays that contain both numbers and strings requires a custom approach since the default sort() method converts all elements to strings. This article explores two effective methods to sort mixed arrays with numbers first (ascending) followed by strings (alphabetical). Problem Statement We need to create a JavaScript function that sorts an array containing both numbers and strings. The requirements are: Numbers should appear before strings Numbers should be sorted in ascending order Strings should be sorted alphabetically Sample Input: ["zebra", 21, "apple", 5, "orange", 13, "banana", 2] Sample ...
Read MoreChecking if a string can be made palindrome in JavaScript
In JavaScript, determining if a string can be made into a palindrome by removing at most one character is a common algorithmic problem. A palindrome reads the same forwards and backwards, like "racecar" or "level". Problem Statement Given a string, we need to check if it can become a palindrome by removing at most one character. The function should return true if possible, false otherwise. Sample Input: "racecar" Sample Output: true Using Two Pointers (Recommended) The most efficient approach uses two pointers starting from both ends of the string, moving towards the center while comparing ...
Read MoreCounting matching substrings in JavaScript
Counting matching substrings in JavaScript is essential for text analysis and string manipulation tasks. This article explores different methods to count subsequences within a string, helping developers choose the most appropriate approach for their needs. Problem Statement We need a JavaScript function that counts subsequences in a given string. The function takes a string "str" and an array of strings "arr" as input. It examines each element in "arr" and determines how many strings are subsequences of "str". A subsequence is formed by removing characters from the original string while maintaining the relative order of remaining characters. ...
Read MoreIn-place Algorithm to Move Zeros to End of List in JavaScript
Moving zeros to the end of an array while maintaining the order of non-zero elements is a common programming problem in JavaScript. This article explores two efficient approaches to solve this problem: the in-place two-pointer method and the count-and-fill approach. Problem Statement Given an array containing integers including zeros, we need to move all zeros to the end while preserving the relative order of non-zero elements. The goal is to achieve this efficiently, ideally in a single pass through the array. For example, given the input array: [2, 5, 0, 1, 0, 8, 0, 3] ...
Read MoreLargest sum of subarrays in JavaScript
Finding the maximum sum subarray is a classic algorithmic problem in JavaScript. Given an array of numbers, we need to find the contiguous subarray with the largest sum. This problem is commonly known as the Maximum Subarray Problem and has practical applications in data analysis and optimization. Problem Statement Given an array of integers, find the contiguous subarray with the maximum sum. For example, given the array: [-3, 4, 2, -1, 6, -5, 3, -2, 7, 1] The maximum subarray sum would be: 15 This comes from the subarray [4, ...
Read MoreNumber pattern in JavaScript
Number patterns in JavaScript are essential for developers who want to create visually appealing outputs and understand algorithmic thinking. These patterns involve generating sequences of numbers that follow specific rules or arrangements, helping developers grasp mathematical concepts that underlie complex algorithms. What are Number Patterns? Number patterns are sequences of numbers arranged in specific formations, typically displayed in triangular or pyramid structures. Each pattern follows a mathematical rule that determines how numbers are positioned and incremented. Pattern 1: Zero-Padded Triangle This pattern creates a triangular structure where each row contains numbers from 1 to the row ...
Read MorePicking index randomly from array in JavaScript
In JavaScript, randomly selecting an index from an array is a common requirement for applications that need unpredictable behavior, such as shuffling content, random sampling, or creating dynamic user experiences. This article explores several effective methods to achieve this functionality. Problem Statement We need an efficient way to randomly select an index from a JavaScript array, where each index has an equal probability of being chosen. JavaScript doesn't provide a built-in method for this, so we'll implement our own solutions. Sample Input: Consider an array: let arr = [12, 45, 7, 89, 34, 56] Sample Output: ...
Read More