Here is our sample array of object, consider each object as representing some page of a multipage website, each object have a next property (unless it represents the last page) that points to some id of another object and a previous property (unless it represents the first page) that points to some id of its previous object.These objects are all positioned randomly right now, our job is to sort them to their correct position −let arr = [ { id: "1325asdfasdasd", next: "5345341fgdfgdd", previous:"545234123fsdfd" }, { id: "das987as9dya8s", next: "3j12k3b1231jkj" }, { id: "89ad8sasds9d8s", previous: "1j3b12k3jbasdd" }, ... Read More
Let the following be the array to be sorted by date and isImportant. All the objects with isImportant property true rank higher than any any of the object with isImportant false and both the groups sorted according to the date property.Following is our array −const array = [{ id: 545, date: 591020824000, isImportant: false, }, { id: 322, date: 591080224000, isImportant: false, }, { id: 543, bdate: 591080424000, isImportant: true, }, { id: 423, date: 591080225525, isImportant: false, }, { id: 135, date: 591020225525, isImportant: ... Read More
We are given an array of strings and another string for which we are required to search in the array. We can filter the array checking whether it contains all the characters that user provided through the input.The code for doing the same would be −ExampleSolution 1const deliveries = ["14/02/2020, 11:47, G12, Kalkaji", "13/02/2020, 11:48, A59, Amar Colony"]; const input = "g12, kal"; const pn = input.split(" "); const requiredDeliveries = deliveries.filter(delivery => pn.every(p => delivery.toLowerCase() .includes(p.toLowerCase()))); console.log(requiredDeliveries);OutputThe output in console −["14/02/2020, 11:47, G12, Kalkaji"]In another and a bit better approach we can remove the step of splitting the input ... Read More
As of the official String.prototype.split() method there exist no way to start splitting a string from index 1 or for general from any index n, but with a little tweak in the way we use split(), we can achieve this functionality.We followed the following approach −We will create two arrays −One that is splitted from 0 to end --- ACTUALSecond that is splitted from 0 TO STARTPOSITION --- LEFTOVERNow, we iterate over each element of leftover and splice it from the actual array. Thus, the actual array hypothetically gets splitted from STARTINDEX to END.Exampleconst string = 'The quick brown fox ... Read More
To flat a JavaScript array of objects into an object, we created a function that takes array of object as only argument. It returns a flattened object with key append by its index. The time complexity is O(mn) where n is the size of array and m is the number of properties in each object. However, its space complexity is O(n) where n is the size of actual array.Example//code to flatten array of objects into an object //example array of objects const notes = [{ title: 'Hello world', id: 1 }, { title: 'Grab a coffee', ... Read More
To find the maximum element in an array, the PHP code is as follows −Example Live DemoOutputThe highest value of the array is91A function named ‘get_max_value()’ is defined, that takes an array as parameter. Inside this function, the count function is used to find the number of elements in the array, and it is assigned to a variable −$n = count($my_array);The first element in the array is assigned to a variable, and the array is iterated over, and adjacent values in the array are compared, and the highest value amongst all of them is given as output −$max_val = $my_array[0]; for ... Read More
To check if a given number is present in an infinite series or not, the PHP code is as follows −Example Live DemoOutputThe number is not present in the infinite seriesAbove, three variables are defined, and the function is called by passing these three values −$m = 3; $n = 5; $o = 9;A function named ‘contains_val’ is defined, that takes these three variables. The first two variables are compared, and if they are equal, ‘true’ is returned. Otherwise, if the different between the first two variables with the product of the third number is greater than 0 and the different ... Read More
To find the first ‘n’ numbers that are missing in an array, the PHP code is as follows −Example Live DemoOutputThe missing values of the array are 1 2 3 4 5In the above code, a function named ‘missing_values´ is defined, that takes the array, length, and the first few numbers that are missing from the array.A variable is assigned to 0 and checked if the first few numbers that need to be found is 0 or more. If it is 0, it is incremented.A count is assigned to 0, and curr value is assigned to 1. Next, the count value ... Read More
To change date format in PHP, the code is as follows −Example Live DemoOutput1970-01-01 00:00:00A function named ‘string_convert’ is used in PHP that takes a date as a parameter. The ‘strtotime’ function is used to convert English text timing into a UNIX timestamp −$sec = strtotime($my_date); $my_date = date("Y-m-d H:i", $sec); $my_date = $my_date . ":00";This date is printed on the screen. Outside the function, the date time is defined, and the function is called on this parameter and the output is displayed on the screen −echo $my_date;
To find the minimum element in an array, the PHP code is as follows −Example Live DemoOutputThe lowest value of the array is 0A function named ‘get_min_value()’ takes an array as parameter. Inside this function, the count function is used to find the number of elements in the array, and it is assigned to a variable −$n = count($my_array);The first element in the array is assigned to a variable, and the array is iterated over, and adjacent values in the array are compared, and the lowest value amongst all of them is given as output −$min_val = $my_array[0]; for ($i = ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP