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
Articles on Trending Technologies
Technical articles with clear explanations and examples
Complicated array grouping JavaScript
Suppose we have an array of objects like this −const arr = [ {userId: "3t5bsFB4PJmA3oTnm", from: 1, to: 6}, {userId: "3t5bsFB4PJmA3oTnm", from: 7, to: 15}, {userId: "3t5bsFB4PJmA3oTnm", from: 172, to: 181}, {userId: "3t5bsFB4PJmA3oTnm", from: 182, to: 190} ];We are required to write a JavaScript function that takes in one such array. The function should group overlapping objects based on their "from" and "to" property into a single object like this −const output = [ {userId: "3t5bsFB4PJmA3oTnm", from: 1, to: 15}, {userId: "3t5bsFB4PJmA3oTnm", from: 172, to: 190} ];Exampleconst arr = [ {userId: "3t5bsFB4PJmA3oTnm", from: ...
Read MoreLongest distance between 1s in binary JavaScript
We are required to write a JavaScript function that in a positive integer, say n. The function should find and return the longest distance between any two adjacent 1's in the binary representation of n.If there are no two adjacent 1's, then we have to return 0.Two 1's are adjacent if there are only 0's separating them (possibly no 0's). The distance between two 1's is the absolute difference between their bit positions. For example, the two 1's in "1001" have a distance of 3.For example −If the input is 22, then the output should be 2, because, The binary ...
Read MoreCheck if a value exists in an array and get the next value JavaScript
We are required to write a JavaScript function that takes in an array of literals as the first argument and a search string as the second argument.The function should the array for the that search string. If that string exists in the array, we should return its next element from the array, otherwise we should return false.Exampleconst arr = ["", "comp", "myval", "view", "1"] const getNext = (value, arr) => { const a = [undefined].concat(arr) const p = a.indexOf(value) + 1; return a[p] || false; } console.log(getNext('comp', arr)); console.log(getNext('foo', arr));OutputAnd the output in the console will be ...
Read MoreGet all methods of any object JavaScript
We are required to write a program (function) that takes in an object reference and returns an array of all the methods (member functions) that lives on that object.We are only required to return the methods in the array and not any other property that might have value of type other than a function.We will use the Object.getOwnPropertyNames functionThe Object.getOwnPropertyNames() method returns an array of all properties (enumerable or not) found directly upon a given object. And then we will filter the array to contain property of data type 'function' only.Exampleconst returnMethods = (obj = {}) => { const ...
Read MoreHow to unflatten a JavaScript object in a daisy-chain/dot notation into an object with nested objects and arrays?
Suppose, we have an object like this −const obj = { "firstName": "John", "lastName": "Green", "car.make": "Honda", "car.model": "Civic", "car.revisions.0.miles": 10150, "car.revisions.0.code": "REV01", "car.revisions.0.changes": "", "car.revisions.1.miles": 20021, "car.revisions.1.code": "REV02", "car.revisions.1.changes.0.type": "asthetic", "car.revisions.1.changes.0.desc": "Left tire cap", "car.revisions.1.changes.1.type": "mechanic", "car.revisions.1.changes.1.desc": "Engine pressure regulator", "visits.0.date": "2015-01-01", "visits.0.dealer": "DEAL-001", "visits.1.date": "2015-03-01", "visits.1.dealer": "DEAL-002" };We are required to write a JavaScript function that takes in one such object and unflattens it into nested objects and arrays.Therefore, ...
Read MoreFind the least duplicate items in an array JavaScript
We are required to write a JavaScript function that takes in an array of literals which may contain some duplicate values.The function should return an array of all those elements that are repeated for the least number of times.For example− If the input array is −const arr = [1, 1, 2, 2, 3, 3, 3];Then the output should be −const output = [1, 2];because 1 and 2 are repeated for the least number of times (2)Exampleconst arr = [1, 1, 2, 2, 3, 3, 3]; const getLeastDuplicateItems = (arr = []) => { const hash = Object.create(null); let ...
Read MoreTransforming array to object JavaScript
Suppose we have an array of strings like this −const arr = [ 'type=A', 'day=45' ];We are required to write a JavaScript function that takes in one such array. The function should construct an object based on this array. The object should contain a key/value pair for each string in the array.For any string, the part before '=' becomes the key and the part after it becomes the value.Exampleconst arr = [ 'type=A', 'day=45' ]; const arrayToObject = (arr = []) => { const obj = {}; for (let i = 0; i < arr.length; i++) { ...
Read MoreFinding the majority element of an array JavaScript
We are given an array of size n, and we are required to find the majority element. The majority element is the element that appears more than [ n/2 ] times.Exampleconst arr = [2, 4, 2, 2, 2, 4, 6, 2, 5, 2]; const majorityElement = (arr = []) => { const threshold = Math.floor(arr.length / 2); const map = {}; for (let i = 0; i < arr.length; i++) { const value = arr[i]; map[value] = map[value] + 1 || 1; if (map[value] > threshold) return value }; return false; }; console.log(majorityElement(arr));OutputAnd the output in the console will be −2
Read MoreFinding sum of a range in an array JavaScript
We are required to write an Array function (functions that lives on Array.prototype object). The function should take in a start index and an end index and it should sum all the elements from start index to end index in the array (including both start and end)Exampleconst arr = [1, 2, 3, 4, 5, 6, 7]; const sumRange = function(start = 0, end = 1){ const res = []; if(start > end){ return res; }; for(let i = start; i
Read MoreDynamic Programming: Is second string subsequence of first JavaScript
We are given two strings str1 and str2, we are required to write a function that checks if str1 is a subsequence of str2.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters.For example, "ace" is a subsequence of "abcde" while "aec" is notExampleconst str1 = 'ace'; const str2 = 'abcde'; const isSubsequence = (str1, str2) => { let i=0; let j=0; while(i
Read More