Object Oriented Programming Articles

Page 176 of 589

Add two array keeping duplicates only once - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 156 Views

When working with two arrays, you often need to merge them while keeping only unique values. This operation combines arrays and removes duplicates in a single step. const arr1 = [2, 4, 5, 3, 7, 8, 9]; const arr2 = [1, 4, 5, 2, 3, 7, 6]; We need to write a JavaScript function that takes two arrays and returns a new array with all duplicates removed (each element appears only once). Using Set with Spread Operator (Recommended) The most efficient approach uses ES6 Set to automatically remove duplicates: const arr1 = ...

Read More

Calculate difference between circumference and area of a circle - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 246 Views

In this tutorial, we'll learn how to calculate the difference between the area and circumference of a circle using JavaScript. This is a common mathematical problem that demonstrates basic geometry calculations and JavaScript functions. Circle Formulas Before we start coding, let's review the formulas: Area of a circle: π × r² Circumference of a circle: 2 × π × r Where r is the radius of the circle and π (pi) is approximately 3.14159. JavaScript Implementation We'll create a function that takes the radius as input and returns the absolute difference between area ...

Read More

Program to pick out duplicate only once - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 712 Views

We have an array of literals that contains some duplicate values appearing for many times like this: const arr = [1, 4, 3, 3, 1, 3, 2, 4, 2, 1, 4, 4]; We are required to write a JavaScript function that takes in this array and pick out all the duplicate entries from the original array and only once. So, for the above array, the output should be: [1, 4, 3, 2] Method 1: Using indexOf and lastIndexOf This approach checks if an element's first and last occurrence positions are ...

Read More

Transpose of a two-dimensional array - JavaScript

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

The transpose of a matrix (2-D array) is simply a flipped version of the original matrix. We can transpose a matrix by switching its rows with its columns. For example, if we have a 3×3 matrix where the first row is [1, 1, 1], after transposing, the first column becomes [1, 1, 1]. Understanding Matrix Transpose Let's visualize how transposition works with a simple example: Original Matrix: [1, 1, 1] [2, 2, 2] [3, 3, 3] Transposed Matrix: [1, 2, 3] [1, 2, 3] [1, 2, 3] Method 1: In-Place Transpose ...

Read More

Implement divide & conquer logic in JavaScript to implement QuickSort

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 692 Views

We are required to write a JavaScript function that takes in an array of numbers and uses the quick sort algorithm to sort it. QuickSort Algorithm QuickSort is a divide and conquer algorithm that works by selecting a 'pivot' element from the array and partitioning other elements into two sub-arrays according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively. How It Works The algorithm follows these steps: Choose a pivot element (typically the middle element) Partition the array so elements smaller than pivot go to the ...

Read More

Find all prime factors of a number - JavaScript

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

We are required to write a JavaScript function that takes in a number and returns an array of all the prime numbers that exactly divide the input number. For example, if the input number is 18, the prime factors are 2 and 3 because 18 = 2 × 3². Then the output should be − [2, 3] Understanding Prime Factorization Prime factorization breaks down a number into its prime components. A prime factor is a prime number that divides the original number exactly. Method 1: Using Helper Function to Check Primes ...

Read More

Largest and smallest word in a string - JavaScript

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

We need to write a JavaScript function that takes a string and returns an array containing the smallest and largest words from the string based on their length. For example, if we have the string: const str = "Hardships often prepare ordinary people for an extraordinary destiny"; The output should be: const output = ["an", "extraordinary"]; The word "an" has 2 characters (smallest) and "extraordinary" has 13 characters (largest). Example Here's the complete implementation: const str = "Hardships often prepare ordinary people for an extraordinary destiny"; ...

Read More

Load a text file and find number of characters in the file - JavaScript

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

Suppose we have a data.txt file that lives in the same directory as our NodeJS file. Suppose the content of that file is − Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s. We are required to write a ...

Read More

Count number of factors of a number - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 514 Views

We are required to write a JavaScript function that takes in a number and returns the count of numbers that exactly divides the input number. For example − If the number is 12, then its factors are − 1, 2, 3, 4, 6, 12 Therefore, the output should be 6. Method 1: Basic Approach This method checks every number from 1 to the given number to see if it divides evenly: function countFactorsBasic(num) { let count = 0; for (let i = 1; i { let count = 0; let flag = 2; while (flag

Read More

JavaScript - Check if array is sorted (irrespective of the order of sorting)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 548 Views

In JavaScript, checking if an array is sorted regardless of order (ascending or descending) requires comparing adjacent elements to determine if they follow a consistent pattern. We need to identify whether the array is sorted in ascending order, descending order, or not sorted at all. Understanding the Problem An array can be considered sorted if all elements follow either: Ascending order: each element is greater than or equal to the previous one Descending order: each element is less than or equal to the previous one Method 1: Check Both Directions This approach checks if ...

Read More
Showing 1751–1760 of 5,881 articles
« Prev 1 174 175 176 177 178 589 Next »
Advertisements