Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Object Oriented Programming Articles
Page 41 of 589
Finding degree of subarray in an array JavaScript
The degree of an array is defined as the maximum frequency of any element in the array. Finding the shortest subarray with the same degree is a common programming problem that involves tracking element frequencies and their positions. const arr = [1, 2, 3, 3, 5, 6, 4, 3, 8, 3]; console.log("Array:", arr); console.log("Degree of array: 4 (element 3 appears 4 times)"); Array: [ 1, 2, 3, 3, 5, 6, 4, 3, 8, 3 ] Degree of array: 4 (element 3 appears 4 times) Our task is to find the length of the ...
Read MoreLongest subarray which only contains strictly increasing numbers JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument. The function should then return the length of the longest continuous subarray from the array that only contains elements in a strictly increasing order. A strictly increasing sequence is the one in which any succeeding element is greater than all its preceding elements. Example const arr = [5, 7, 8, 12, 4, 56, 6, 54, 89]; const findLongest = (arr) => { if(arr.length == 0) { ...
Read MoreRelative sorting in JavaScript
Suppose, we have two arrays, let's say arr1 and arr2. The elements of arr2 are distinct, and all elements in arr2 are also in arr1. We are required to write a JavaScript function that takes in two such arrays and sorts the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order. For example− If the two input arrays are − const arr1 = [2, 3, 1, 3, 2, 4, 6, ...
Read MoreTransform tree from DB format to JSON format in JavaScript
When working with database records, data is often stored in a flat structure with parent-child relationships defined by IDs. This tutorial shows how to transform such flat data into a hierarchical tree structure in JavaScript. The Problem Suppose we have an array of objects representing geographical regions from a database: const arr = [ {"id":7, "name":"Kuwait", "parentId":2}, {"id":4, "name":"Iraq", "parentId":2}, {"id":10, "name":"Qatar", "parentId":2}, {"id":2, "name":"Middle East", "parentId":1}, {"id":3, "name":"Bahrain", "parentId":2}, {"id":6, "name":"Jordan", ...
Read MoreCreating permutations to reach a target number, but reuse the provided numbers JavaScript
We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a target sum Number as the second argument. The function should return an array of all those subarrays from the original array whose elements sum to make the target sum. We can use a single number multiple times to achieve the sum. For example − If the input array and number are − const arr = [1, 2, 4]; const sum = 4; then the output should be − [ ...
Read MoreCreating an array of objects based on another array of objects JavaScript
In JavaScript, you can create a new array of objects based on an existing array by transforming each object's structure. This is commonly done using the map() method to extract specific properties and create new object formats. Suppose we have an array of objects containing user data like this: const arr = [ {"user":"dan", "liked":"yes", "age":"22"}, {"user":"sarah", "liked":"no", "age":"21"}, {"user":"john", "liked":"yes", "age":"23"}, ]; console.log("Original array:", arr); Original array: [ { user: 'dan', liked: 'yes', age: '22' }, { ...
Read MoreThe algorithm problem - Backtracing pattern in JavaScript
Backtracking is a powerful algorithmic pattern for solving constraint satisfaction problems. In this grid path problem, we need to find all possible paths from start to end that visit every walkable square exactly once. Problem Definition Given a 2D grid with different square types, find the number of unique paths from start to end: 1 represents the starting square (exactly one) 2 represents the ending square (exactly one) 0 represents empty squares we can walk over -1 represents obstacles we cannot walk over ...
Read MoreFilter unique array values and sum in JavaScript
In JavaScript, you may need to filter arrays that contain duplicate values and combine their numeric values. This is common when processing data like product orders, transactions, or inventory records. Problem Statement Given an array of arrays where each subarray has three elements [id, name, amount], we need to: Remove duplicate entries based on the first element (id) Sum the third element (amount) for matching entries const arr = [[12345, "product", "10"], [12345, "product", "15"], [1234567, "other", "10"]]; The expected output should combine the duplicate entries and sum their amounts: ...
Read MoreGroup all the objects together having the same value for the '_id' key in JavaScript
Suppose we have an array of objects like this − const arr = [ {_id : "1", S : "2"}, {_id : "1", M : "4"}, {_id : "2", M : "1"}, {_id : "" , M : "1"}, {_id : "3", S : "3"} ]; We are required to write a JavaScript function that takes in one such array and groups all the objects together that have the same value for the '_id' key. Therefore, the final output should ...
Read MoreHow to create a third object from two objects using the key values in JavaScript?
In JavaScript, you can create a third object from two objects by mapping key values between them. This is useful when you have one object containing arrays of keys and another object with corresponding values. Problem Statement Given two objects where the first contains arrays of keys and the second contains key-value pairs, we need to calculate totals for each category: const obj1 = { positive: ['happy', 'excited', 'joyful'], negative: ['depressed', 'sad', 'unhappy'] }; const obj2 = { happy: 6, excited: 1, unhappy: 3 }; ...
Read More