Object Oriented Programming Articles

Page 22 of 589

Sum of all the non-repeating elements of an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 493 Views

To find the sum of non-repeating elements in an array, we need to identify elements that appear exactly once and add them together. Problem Statement Given an array of numbers, we want to calculate the sum of all elements that appear only once in the array. const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14]; console.log("Input array:", arr); Input array: [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14] In this array, the non-repeating elements are: 23, 24, 33, 44, 87. Their ...

Read More

Checking the validity of parentheses in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

We are required to write a JavaScript function that takes in a string containing just the characters: '(', ')', '{', '}', '[' and ']' Our function should determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. ...

Read More

Pair of (adjacent) elements of an array whose sum is lowest JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 533 Views

We are required to write a JavaScript function that takes in an array of numbers. The function should return a subarray of two adjacent elements from the original array whose sum is the least amongst all adjacent pairs of the array. If the length of the array is less than 2, we should return boolean false. Example Input For example, if the input array is: const arr = [41, 44, -12, 13, -23, 1, 5, -4, 2, 2]; Here, the sum of pair [-23, 1] is -22 which is the least for any ...

Read More

Dash separated cartesian product of any number of arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 222 Views

We need to write a JavaScript function that takes any number of arrays and computes their cartesian product, with elements separated by dashes. The cartesian product combines every element from the first array with every element from the second array, and so on. What is Cartesian Product? The cartesian product of two or more sets is the set of all possible combinations where we pick one element from each set. For example, if we have arrays ['a', 'b'] and ['1', '2'], the cartesian product would be ['a-1', 'a-2', 'b-1', 'b-2']. Implementation Here's how we can implement ...

Read More

Removing a specific substring from a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 465 Views

We are given a main string and a substring, our job is to create a function, let's say removeString() that takes in these two arguments and returns a version of the main string which is free of the substring. Here, we need to remove the separator from a string, for example: this-is-a-string Using split() and join() Method The most common approach is to split the string by the substring and then join the parts back together: const removeString = (string, separator) => { // we split the string ...

Read More

Counting number of vowels in a string with JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 747 Views

We are required to write a JavaScript function that takes in a string. The function should count the number of vowels present in the string. The function should prepare an object mapping the count of each vowel against them. Example The code for this will be − const str = 'this is an example string'; const vowelCount = (str = '') => { const splitString = str.split(''); const obj = {}; const vowels = "aeiou"; splitString.forEach((letter) => { ...

Read More

Checking for the Gapful numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 146 Views

A gapful number is a special type of number that meets specific criteria. Understanding gapful numbers can be useful in mathematical programming and number theory applications. What is a Gapful Number? A number is considered gapful when: It has at least three digits, and It is exactly divisible by the number formed by combining its first and last digits Examples 1053 is a gapful number because it has 4 digits and is divisible by 13 (first digit 1 + last digit 3). 135 is ...

Read More

Natural Sort in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 917 Views

Natural sort in JavaScript refers to sorting arrays containing mixed data types (numbers and strings) in a way that numbers come first in ascending order, followed by strings in alphabetical order. Problem Statement When sorting mixed arrays with the default sort() method, JavaScript converts everything to strings, leading to incorrect ordering. We need a custom sorting function that handles numbers and strings separately. Example Input and Expected Output Let's say this is our array: const arr = [1, 'fdf', 'afv', 6, 47, 7, 'svd', 'bdf', 9]; console.log("Original array:", arr); Original array: ...

Read More

Sum identical elements within one array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 331 Views

We are required to write a JavaScript function that takes in an array of numbers. The array might contain some repeating / duplicate entries within it. Our function should add all the duplicate entries and return the new array thus formed. Example The code for this will be − const arr = [20, 20, 20, 10, 10, 5, 1]; const sumIdentical = (arr = []) => { let map = {}; for (let i = 0; i < arr.length; i++) { ...

Read More

Building frequency map of all the elements in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

Building a frequency map (also called a frequency counter) is a common programming task that helps count how many times each element appears in an array. This technique is useful for data analysis, finding duplicates, or solving algorithmic problems. A frequency map returns an object where each unique element from the array becomes a key, and its corresponding value represents how many times that element appears. Basic Approach Using forEach() The most straightforward method is to iterate through the array and build an object that tracks the count of each element: const arr = [2, ...

Read More
Showing 211–220 of 5,881 articles
« Prev 1 20 21 22 23 24 589 Next »
Advertisements