AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 473 of 840

Find the middle element of an array using recursion JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 529 Views

We are required to write an array function, say findMiddle that returns the middlemost element of the array without accessing its length property and without using any kind of built-in loops. If the array contains an odd number of elements, we return the one, middlemost element, or if the array contains an even number of elements, we return an array of two middlemost elements. So, let's write the code for this function. As you've already guessed, we will be making use of recursion to find these elements. The code for the recursive function will be — How It ...

Read More

Program to find uncommon elements in two arrays - JavaScript

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

Finding uncommon elements in two arrays means identifying elements that exist in one array but not in both. This is also known as finding the symmetric difference between two arrays. Problem Definition Given two arrays, we need to find elements that are present in either the first array or the second array, but not in both arrays. const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34]; console.log("Array 1:", arr1); console.log("Array 2:", arr2); Array 1: [12, 54, 2, 4, 6, 34, ...

Read More

How to create a URL object using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 560 Views

The URL object in JavaScript provides a convenient way to parse and manipulate URLs. It extracts components like protocol, host, pathname, and search parameters from URL strings. Syntax let url = new URL(urlString); let url = new URL(urlString, baseURL); Parameters urlString - The URL string to parse baseURL - (Optional) Base URL for relative URLs Example URL Object Example body { ...

Read More

Changing an array in place using splice() JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 235 Views

The splice() method allows you to modify arrays in place by removing, adding, or replacing elements. This article demonstrates how to use splice() to remove duplicate elements that exceed a specified count limit. The Problem We need to write a function that takes an array and a number n, then removes elements that appear more than n times while preserving the order of remaining elements. Solution Using splice() We'll track element counts using a hashmap and use splice() to remove excess occurrences during iteration: const arr = [7, 26, 21, 41, 43, 2, 26, ...

Read More

Creating an associative array in JavaScript with push()?

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

An associative array in JavaScript is essentially an object that uses string keys to store arrays of values. You can create this structure by combining forEach() loops with the push() method to group related data. Example: Grouping Students by ID Here's how to create an associative array that groups student names by their student ID: var studentDetails = [ { studentId: 1, studentName: "John" }, { ...

Read More

Count appearances of a string in another - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 182 Views

We are required to write a JavaScript function that takes in two strings and returns the count of the number of times the first string appears in the second string. Let's say our string is: const main = 'This is the is main is string'; We have to find the appearance of the below string in the above "main" string: const sub = 'is'; Using Regular Expression with replace() Let's write the code for this function using a regular expression approach: const main = 'This is the is ...

Read More

How to copy text to the clipboard with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 678 Views

To copy text to the clipboard in JavaScript, we can use both modern and legacy approaches. The modern navigator.clipboard API is recommended, while the older document.execCommand() method provides fallback support. Modern Approach: Using navigator.clipboard API The navigator.clipboard.writeText() method is the modern standard for copying text. It returns a Promise and works asynchronously. button { border: none; ...

Read More

Explain shorthand functions in JavaScript?

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

Arrow functions, also known as shorthand functions, were introduced in ES2015 and allow us to write functions in a shorter, more concise way. They don't have their own binding to this and inherit this from the surrounding context. Basic Syntax Arrow functions use the => syntax instead of the function keyword: // Regular function function add(a, b) { return a + b; } // Arrow function const add = (a, b) => a + b; Different Arrow Function Forms ...

Read More

Algorithm to dynamically populate JavaScript array with zeros before and after values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 187 Views

We are given a months array, which contains elements less than 12, where each element will be between 1 and 12 (both inclusive). Our job is to take this array and create a full months array with 12 elements. If the element is present in the original array we use that element, otherwise we use 0 at that place. For example: Input → [5, 7, 9] Output → [0, 0, 0, 0, 5, 0, 7, 0, 9, 0, 0, 0] Now, let's write the code: Using Array.includes() Method const months = [6, ...

Read More

Group array by equal values JavaScript

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

Let's say, we have an array of string/number literals that contains some duplicate values like this: const array = ['day', 'night', 'afternoon', 'night', 'noon', 'night', 'noon', 'day', 'afternoon', 'day', 'night']; We are required to write a function groupSimilar() that takes in this array and returns a new array where all the repeating entries are grouped together in a subarray as the first element and their total count in the original array as the second element. So, for this example, the output should be: [ [ 'day', 3 ], ...

Read More
Showing 4721–4730 of 8,392 articles
« Prev 1 471 472 473 474 475 840 Next »
Advertisements