Object Oriented Programming Articles

Page 184 of 589

Alternatively merging two arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 856 Views

We are required to write a JavaScript function that takes in two arrays and merges the arrays taking elements alternatively from the arrays. For example, if the two arrays are: const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3]; Then the output should be: const output = [4, 2, 3, 1, 2, 6, 5, 8, 6, 9, 8, 4, 9, 3]; Method 1: Using Index-Based Logic This approach uses a single loop and alternates between arrays based on even/odd ...

Read More

Sum all similar elements in one array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 476 Views

We are required to write a JavaScript function that takes in an array of numbers and sums all the identical numbers together at one index, removing duplicates while preserving the first occurrence position. For example, if we have repeated numbers in an array, we want to combine them into a single sum at the position where they first appear. Problem Statement If the input array is: const arr = [20, 10, 15, 20, 15, 10]; Then the output should be: const output = [40, 20, 30]; Here, 20 appears ...

Read More

Find the shortest string in an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 794 Views

We are required to write a JavaScript function that takes in an array of strings and returns the shortest string in the array. This is a common programming problem that can be solved efficiently using different approaches. We will explore multiple methods to find the shortest string, from simple loops to functional programming approaches. Using for Loop The most straightforward approach uses a for loop to iterate through the array and track the shortest string: const arr = ['this', 'can', 'be', 'some', 'random', 'sentence']; function findShortestString(strings) { if (strings.length === ...

Read More

Retrieve user id from array of object - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 976 Views

Suppose, we have an array of objects where the user names are mapped to some unique ids like this − const arr = [ {"4": "Rahul"}, {"7": "Vikram"}, {"6": "Rahul"}, {"3": "Aakash"}, {"5": "Vikram"} ]; As apparent in the array, same names can have more than one ids but same ids cannot be used to map two different names. We are required to write a JavaScript function that takes in one such array as the first argument and a name ...

Read More

JavaScript: Finding nearest prime number greater or equal to sum of digits - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 228 Views

We are required to write a JavaScript function that takes in a number, finds the sum of its digits and returns a prime number that is just greater than or equal to the sum. Understanding the Problem For a number like 56563, we first calculate the sum of digits: 5 + 6 + 5 + 6 + 3 = 25. Since 25 is not prime (divisible by 5), we find the next prime number, which is 29. Example const num = 56563; const digitSum = (num, sum = 0) => { ...

Read More

Finding special type of numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 244 Views

In the decimal number system, all the real numbers can be divided into two groups: Rational Numbers Irrational Numbers For the scope of this problem we will only discuss the rational numbers. All those numbers which can be written in the p/q form (where q ≠ 0) are called rational numbers, like 14, 4.6, 3.33333... and many more. The rational numbers can further be divided into two groups: Terminating decimal numbers Repeating decimal numbers This categorization is made on the basis ...

Read More

Are array of numbers equal - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 214 Views

We need to write a JavaScript function that takes in two arrays of numbers and checks for their equality based on specific criteria. The arrays will be considered equal if: They contain the same elements and in the same order. The product of all the elements of the first array and second array is equal. Example Arrays The first array of numbers: const first = [3, 5, 6, 7, 7]; The second array of numbers: const second = [7, 5, 3, 7, 6]; ...

Read More

Does this array contain any majority element - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 174 Views

Given an array of numbers, any element of the array will be a majority element if that element appears more than array length's 1/2 times in the array. For example, if the length of array is 7, then if there's any element in the array that appears for at least 4 number of times, it will be considered a majority. It's quite apparent that any particular array can have at most one majority element. We are required to write a JavaScript function that takes in an array of numbers with repetitive values and returns true if there exists ...

Read More

Thrice sum of elements of array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 273 Views

We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as sum of three consecutive elements from the original array. For example, if the input array is: const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Then the output should be: const output = [3, 12, 21, 9]; The function groups elements in sets of three and calculates their sum: (0+1+2=3), (3+4+5=12), (6+7+8=21), and (9+0+0=9) for the remaining element. How It Works The ...

Read More

Finding the rotation of an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 235 Views

We are required to write a JavaScript function that takes in an array and a number n. Our function should rotate the array by n elements, i.e., take n elements from the front and put them to the end. The only condition here is that we have to do this without using any extra space in memory. For example, if the input array is: const arr = [12, 6, 43, 5, 7, 2, 5]; and number n is 3, then the output should be: const output = [5, 7, 2, 5, ...

Read More
Showing 1831–1840 of 5,881 articles
« Prev 1 182 183 184 185 186 589 Next »
Advertisements