AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 343 of 840

Finding elements whose successors and predecessors are in array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 519 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument. The function should construct and return a new array that contains all such elements from the original array whose successor and predecessor are both present in the array. This means, if any element num is in the original array, it should be included in the result array if and only if num - 1 and num + 1 are also present in the array. For example, if the input array is: const arr = ...

Read More

Numbers obtained during checking divisibility by 7 using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 720 Views

In mathematics, we can check if a number is divisible by 7 using a special algorithm. For any number of the form 10a + b (where a is all digits except the last, and b is the last digit), we calculate a - 2b. If this result is divisible by 7, then the original number is also divisible by 7. We repeat this process until we get a number with at most 2 digits, since it's easy to check divisibility by 7 for such small numbers. This article demonstrates how to implement this algorithm in JavaScript and count the ...

Read More

Count of pairs in an array that have consecutive numbers using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 506 Views

Problem We need to write a JavaScript function that takes an array of integers and returns the count of consecutive pairs. A consecutive pair consists of two adjacent elements in the array where one number is exactly one more than the other. Understanding Consecutive Pairs Two numbers are consecutive if their absolute difference is 1. For example, (5, 6), (8, 7), and (-3, -4) are all consecutive pairs. Solution We'll iterate through the array in steps of 2, treating each pair of adjacent elements as a potential consecutive pair: const arr = [1, ...

Read More

Checking for majority element in a sorted array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 241 Views

A majority element in an array of length l is an element that appears more than l/2 times. In a sorted array, if a majority element exists, it must occupy the middle position since it spans over more than half the array. Problem Definition We need to write a JavaScript function isMajority() that takes a sorted array and a number, returning true if that number is the majority element, false otherwise. Key Insight for Sorted Arrays In a sorted array, if there exists a majority element, it will always be the middle element. This is because ...

Read More

Separating data type from array into groups in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 647 Views

In JavaScript, you often need to group array elements by their data types. This is useful for data analysis, validation, or organizing mixed-type arrays. We'll create a function that separates elements into groups based on their typeof result. Problem We need to write a JavaScript function that takes an array of mixed data types and returns an object where each key represents a data type and its value is an array containing all elements of that type. Solution Here's how to group array elements by their data types: const arr = [1, 'a', [], ...

Read More

Finding the longest consecutive appearance of a character in another string using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 281 Views

Problem We are required to write a JavaScript function that takes in a string as the first argument and a single character as the second argument. Our function should count and return the longest consecutive appearance of the character in the string. Approach We'll iterate through the string and track the current consecutive count and maximum count found so far. When we encounter the target character, we increment the counter. When we encounter a different character, we reset the counter. Example Following is the code − const str = 'abcdaaadse'; const char ...

Read More

Search a complex object by id property in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 489 Views

Suppose we have a complex JSON Object like this − const obj = { "id": "0001", "fieldName": "sample1", "fieldValue": "0001", "subList": [ { "id": 1001, "fieldName": "Sample Child 1", "fieldValue": "1001", "subList": [] }, { "id": 1002, "fieldName": "Sample Child 2", ...

Read More

Meeting Rooms 2 problem in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 403 Views

We will be given an array of arrays, each subarray consists of exactly two elements indicating the start and end time of a meeting. The task of our function is to find the maximum number of meetings one person can take avoiding the conflict of time. The function should finally return this number. For example − If the input array describing meeting times is − const arr = [[5, 40], [10, 20], [25, 35]]; Then the output should be − const output = 2; because it's not possible to ...

Read More

Returning lengthy words from a string using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 156 Views

We are required to write a JavaScript function that takes in a sentence of words and a number. The function should return an array of all words greater than the length specified by the number. Problem Statement Input: const str = 'this is an example of a basic sentence'; const num = 4; Expected Output: const output = [ 'example', 'basic', 'sentence' ]; Because these are the only three words with length greater than 4. Using for Loop The most straightforward approach is to split the string into ...

Read More

Finding the count of total upside down numbers in a range using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 302 Views

Upside Down Numbers Upside down numbers are special numbers that remain identical when rotated 180 degrees. Only certain digits can form upside down numbers: 0, 1, 6, 8, and 9, where 6 becomes 9 and vice versa when rotated. For example, 69 becomes 69 when rotated, and 8108 becomes 8018 (which is not the same, so it's not upside down). Problem Statement We need to write a JavaScript function that takes a range of two numbers and returns the count of all upside down numbers within that range. Understanding Valid Digits When rotated 180 ...

Read More
Showing 3421–3430 of 8,392 articles
« Prev 1 341 342 343 344 345 840 Next »
Advertisements