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
Object Oriented Programming Articles
Page 177 of 589
Twice repetitive word count in a string - JavaScript
We are required to write a JavaScript function that takes in a string that contains some words that are repeated twice, we need to count such words. For example, if the input string is: const str = "car bus jeep car jeep bus motorbike truck"; Then the output should be: 3 The function counts words that appear more than once in the string. In this case, "car", "bus", and "jeep" each appear twice. Using Array Methods Here's a solution that splits the string into words and counts duplicates: ...
Read MoreFind the element that appears once in sorted array - JavaScript
Suppose, we have a sorted array of literals like this − const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9]; We are required to write a JavaScript function that takes in one such array and returns the first number that appears only once in the array. If there is no such number in the array, we should return false. For this array, the output should be 6 Approach 1: Linear Scan with State Tracking This approach uses a boolean flag to track whether we've encountered duplicates of the ...
Read MoreMaximum Possible Sum of Products in JavaScript
We are given two arrays say, arr1 and arr2 of positive numbers. The number of values in both the arrays are the same. We are required to write a function that finds the maximum sum of products of their elements. Each element in arr1 has to be multiplied with exactly one element in arr2 and vice versa such that each element of both the arrays appears exactly once and the sum of product produced is maximum. Problem Understanding For example: if, arr1 = [5, 1, 3, 4, 2] and, arr2 = [8, 10, 9, 7, ...
Read MorePartially reversing an array - JavaScript
Suppose, we have an array of literals like this: const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9]; console.log("Original array:", arr); Original array: [3, 5, 5, 2, 23, 4, 7, 8, 8, 9] We are required to write a JavaScript function that takes in such an array and a number, say n (n must be less than or equal to the length of array). And the function should reverse the first n elements of the array within. For example, if for this array, the number is 4, then ...
Read MoreMove string capital letters to front maintaining the relative order - JavaScript
We need to write a JavaScript function that takes a string with uppercase and lowercase letters and returns a new string with all uppercase letters moved to the front while maintaining their relative order. For example, if the input string is: const str = 'heLLO woRlD'; Then the output should be: const output = 'LLORDhe wol'; Algorithm Explanation The approach uses an array and a tracking index. We iterate through each character, and if it's uppercase, we insert it at the current capital position and increment the index. Lowercase characters ...
Read MoreRemoving 0s from start and end - JavaScript
We are required to write a JavaScript function that takes in a number as a string and returns a new number string with all the leading and trailing 0s removed. For example: If the input is − const strNum = '054954000' Then the output should be − const output = '54954' Using substring() Method This approach uses two pointers to find the first non-zero character from the start and end, then extracts the substring between them. const strNum = '054954000'; const removeZero = (str = '') => ...
Read MoreReverse word starting with particular characters - JavaScript
We need to write a JavaScript function that takes a sentence string and a character, then reverses all words starting with that specific character. For example, if we have the string: const str = 'hello world, how are you'; And we want to reverse words starting with 'h', the output should be: const output = 'olleh world, woh are you'; Notice that "hello" becomes "olleh" and "how" becomes "woh" because both start with 'h'. Solution We'll split the string into words, check each word's first character, and reverse those ...
Read MoreCasting a string to snake case - JavaScript
Snake case is a naming convention where words are separated by underscores (_) and all letters are lowercase. For example, "Hello World" becomes "hello_world". This article demonstrates how to convert any string to snake case using JavaScript. Syntax function toSnakeCase(str) { return str.split(' ') .map(word => word.toLowerCase()) .join('_'); } Example Here's a complete example that converts a sentence to snake case: ...
Read MoreDistance to nearest vowel in a string - JavaScript
We are required to write a JavaScript function that takes in a string with at least one vowel, and for each character in the string we have to map a number representing its nearest distance from a vowel. For example: If the string is − const str = 'vatghvf'; Then the output should be − const output = [1, 0, 1, 2, 3, 4, 5]; How It Works The algorithm works by: Finding all vowel positions in the string For each character, calculating the minimum distance to any vowel ...
Read MoreCalculating resistance of n devices - JavaScript
In Physics, the equivalent resistance of resistors varies based on their connection type. When resistors are connected in series, their resistances add directly: R_series = R1 + R2 + R3 For parallel connections, we use the reciprocal formula: 1/R_parallel = (1/R1) + (1/R2) + (1/R3) We need to create a JavaScript function that calculates equivalent resistance for any number of resistors in either series or parallel configuration. Series Connection Implementation For series resistors, we simply sum all resistance values: const calculateSeries = (...resistors) => { ...
Read More