Object Oriented Programming Articles

Page 30 of 589

Grouping of same kind of numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 211 Views

We have an array of numbers with both negative and non-negative values. Our goal is to count consecutive groups of non-negative numbers (positives and zeros) in the array. const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]; console.log("Array:", arr); Array: [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0] In this array, we need to identify consecutive groups of non-negative numbers. Looking at the array: Index 3: single element 0 forms one group Indices 9-11: elements 0, 1, 0 form another group ...

Read More

Similarities between different strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 190 Views

In JavaScript, finding similarities (intersections) between two arrays of strings is a common operation. We need to write a function that computes the intersection of two string arrays and returns elements that appear in both arrays. Problem Statement Given two arrays of strings, find the common elements that exist in both arrays. Each element in the result should appear as many times as it shows in both arrays. Example If we have these input arrays: arr1 = ['hello', 'world', 'how', 'are', 'you']; arr2 = ['hey', 'world', 'can', 'you', 'rotate']; The expected output ...

Read More

Upper or lower elements count in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 151 Views

When working with arrays of numbers, you may need to count how many elements are above or below a specific threshold value. This is useful for data analysis, filtering, and statistical operations. Consider we have an array of numbers that looks like this: const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78]; We need to write a function that counts how many elements in the array are below and above a given number. For example, if the threshold number is 60: Elements below ...

Read More

Listing all the prime numbers upto a specific number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 241 Views

We are required to write a JavaScript function that takes in a number, say n, and returns an array containing all the prime numbers up to n. For example: If the number n is 24. Then the output should be − const output = [2, 3, 5, 7, 11, 13, 17, 19, 23]; Therefore, let's write the code for this function − Method 1: Using Helper Function for Prime Check This approach uses a helper function to check if a number is prime, then iterates through numbers up to n: ...

Read More

If the element repeats, remove all its instances from array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 145 Views

When working with arrays in JavaScript, sometimes you need to remove all instances of elements that appear more than once, keeping only elements that appear exactly once. This is different from typical deduplication where you keep one instance of each duplicate. For example, if the input is: const arr = [763, 55, 43, 22, 32, 43, 763, 43]; console.log("Original array:", arr); Original array: [ 763, 55, 43, 22, 32, 43, 763, 43 ] The output should be: [ 55, 22, 32 ] Notice that 763 and 43 are ...

Read More

Array flattening using loops and recursion in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 673 Views

Array flattening converts nested arrays into a single-level array. JavaScript provides multiple approaches including loops, recursion, and built-in methods. Problem Overview Given a nested array with mixed data types including falsy values, we need to flatten it completely: const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], [6]]; console.log("Input:", arr); Input: [ [ 1, 2, 3 ], [ 4, 5, [ 5, false, 6, [Array] ] ], [ 6 ] ] Expected output should be a flat array preserving all values including false and null: ...

Read More

Using recursion to remove consecutive duplicate entries from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 548 Views

We are supposed to write a function that takes in an array of number/string literals. The function should remove all the redundant consecutive elements of the array without using extra memory space. For example, if the input array is − const arr = [17, 17, 17, 12, 12, 354, 354, 1, 1, 1]; Then the output should be − const output = [17, 12, 354, 1]; Therefore, let's write the code for this function − How the Algorithm Works The recursive function works by checking each element against its ...

Read More

Detecting the largest element in an array of Numbers (nested) in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 258 Views

We need to find the largest number in a nested array of any depth. This problem requires recursion to traverse through all nested levels and compare each number to find the maximum value. For example, if we have this nested array: const arr = [ 34, 65, 67, [ 43, 76, 87, 23, 56, 7, [ 54, 7, 87, 23, 79, 994, 2 ], ...

Read More

Greatest element in a Multi-Dimensional Array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 195 Views

We have to write a simple function in JavaScript that takes in an array of Numbers (nested to any level) and return the greatest number present in the array. Problem Example If the input array is: const arr = [ 34, 65, 67, [ 43, 76, 87, 23, 56, 7, [ 54, 7, 87, 23, 79, 994, 2 ], ...

Read More

Removing duplicates and inserting empty strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 341 Views

We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end. Problem Description If we find 4 duplicate values we have to remove them all and insert four empty strings at the end. This maintains the original array length while eliminating duplicates. How It Works The algorithm uses reduce() to iterate through the array. For each element, it checks if the current index matches the last occurrence using lastIndexOf(). If yes, it's the final occurrence and gets added to the ...

Read More
Showing 291–300 of 5,881 articles
« Prev 1 28 29 30 31 32 589 Next »
Advertisements