Found 9306 Articles for Object Oriented Programming

Remove duplicates from a array of objects JavaScript

AmitDiwan
Updated on 20-Aug-2020 06:16:51

550 Views

We are required to write a function that removes duplicate objects from an array and returns a new one. Consider one object the duplicate of other if they both have same number of keys, same keys and same value for each key.Let’s write the code for this −We will use a map to store distinct objects in stringified form and once we see a duplicate key we omit it otherwise we push the object into the new array −Exampleconst arr = [    {       "timestamp": 564328370007,       "message": "It will rain today"    },   ... Read More

Replace a letter with its alphabet position JavaScript

AmitDiwan
Updated on 20-Aug-2020 06:13:55

944 Views

We are required to write a function that takes in a string, trims it off any whitespaces, converts it to lowercase and returns an array of numbers describing corresponding characters positions in the english alphabets, any whitespace or special character within the string should be ignored.For example −Input → ‘Hello world!’ Output → [8, 5, 12, 12, 15, 23, 15, 18, 12, 4]The code for this will be −Exampleconst str = 'Hello world!'; const mapString = (str) => {    const mappedArray = [];    str    .trim()    .toLowerCase()    .split("")    .forEach(char => {       const ascii = char.charCodeAt();       if(ascii >= 97 && ascii

Find Max Slice Of Array | JavaScript

AmitDiwan
Updated on 20-Aug-2020 06:12:36

159 Views

Let’s say, we are required to write a function that takes an array as input and returns the maximum slice of the array which contains no more than two different numbers. If we closely examine this problem this involves checking for a stable sub array and iterating over the original array.Therefore, the sliding window algorithm is very suitable for this. The code for solving this problem via sliding window algorithm will be −Exampleconst arr = [1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8, 1, 1 ,1 ,1, 8, 1, 1, 8, 8]; const map ... Read More

JavaScript tracking the differences between elements in an array?

AmitDiwan
Updated on 20-Aug-2020 06:09:53

623 Views

We are given an array of Number literals, and we are required to write a function that returns the absolute difference of two consecutive elements of the array.For example −If input array is [23, 53, 66, 11, 67] Output should be [ 30, 13, 55, 56]Let’s write the code for this problem −We will use a for loop that will start iterating from the index 1 until the end of the array and keep feeding the absolute difference of [i] th and [i -1] th element of the original array into a new array. Here’s the code −Examplevar arr = ... Read More

Binary Search program in JavaScript

AmitDiwan
Updated on 20-Aug-2020 06:09:08

622 Views

Create a function, say binarySearch() that takes in 4 arguments −a sorted Number / String literal arraystarting index of array (0)ending index of array (length - 1)number to be searchedIf the number exists in the array, then the index of the number should be returned, otherwise -1 should be returned. Here is the full code −Exampleconst arr = [2, 4, 6, 6, 8, 8, 9, 10, 13, 15, 17, 21, 24, 26, 28, 36, 58, 78, 90]; //binary search function //returns the element index if found otherwise -1 const binarySearch = (arr, start, end, num) => {    const mid ... Read More

How to compare two string arrays, case insensitive and independent about ordering JavaScript, ES6

AmitDiwan
Updated on 20-Aug-2020 06:07:54

626 Views

We are required to write a function, say isEqual() that takes in two strings as argument and checks if they both contains the same characters independent of their order and case.For example −const first = 'Aavsg'; const second = 'VSAAg'; isEqual(first, second); //trueMethod: 1 Using arraysIn this method we convert the strings into arrays, make use of the Array.prototype.sort() method, convert them back into strings and check for equality.The code for this will be −Exampleconst first = 'Aavsg'; const second = 'VSAAg'; const stringSort = function(){    return this.split("").sort().join(""); } String.prototype.sort = stringSort; const isEqual = (first, second) => first.toLowerCase().sort() ... Read More

Sort array based on presence of fields in objects JavaScript

AmitDiwan
Updated on 20-Aug-2020 06:06:19

217 Views

Let’s say we have the following array of objects −const people = [{    firstName: 'Ram',    id: 301 }, {    firstName: 'Shyam',    lastName: 'Singh',    id: 1016 }, {    firstName: 'Dinesh',    lastName: 'Lamba',    id: 231 }, {    id: 341 }, {    firstName: 'Karan',    lastName: 'Malhotra',    id: 441 }, {    id: 8881 }, {    firstName: 'Vivek',    id: 301 }];We are required to sort this array so that the object with both firstName and lastName property appears first then the objects with firstName or lastName and lastly the objects ... Read More

Get closest number out of array JavaScript

Nikitasha Shrivastava
Updated on 14-Aug-2023 18:01:38

428 Views

In the above problem statement we are required to get the closest number of the given target out of the array. And we have to produce the code with the help of Javascript. Understanding the Problem The problem at hand is to find the closest number of the given target value within the array. So this problem can be achieved using a logical algorithm which will iterate through the array and compare every item to the target value. And then we will determine the closest number. So we will use Javascript to develop the solution. ... Read More

Find all subarrays with sum equal to number? JavaScript (Sliding Window Algorithm)

AmitDiwan
Updated on 20-Aug-2020 06:01:40

387 Views

We are given an array of numbers and a number; our job is to write a function that returns an array of all the sub arrays which add up to the number provided as second argument.For example −const arr = [23, 5, 1, 34, 12, 67, 9, 31, 6, 7, 27]; const sum = 40; console.log(requiredSum(arr, sum));Should output the following array −[ [ 5, 1, 34 ], [ 9, 31 ], [ 6, 7, 27 ] ]Because each of these 3 sub arrays add up to 40.The Sliding Window Algorithm (Linear Time)This algorithm is mostly used when we are required ... Read More

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

AmitDiwan
Updated on 20-Aug-2020 05:59:48

81 Views

We are given a months array, which 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 at that place.For example −Intput → [5, 7, 9] Output → [0, 0, 0, 0, 5, 0, 7, 0, 9, 10, 0, 0]Now, let’s write the code −Exampleconst months = [6, 7, 10, 12]; const completeMonths = (arr) => {    const completed = [];    for(let i = 1; i

Advertisements