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 More
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 More
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
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
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
We are required to write a JavaScript function that takes in an array of objects of dates like this −const arr = [ {date: "2016-06-08 18:10:00"}, {date: "2016-04-26 20:01:00"}, {date: "2017-02-06 14:38:00"}, {date: "2017-01-18 17:30:21"}, {date: "2017-01-18 17:24:00"} ];We are required to write a JavaScript function that takes in one such array. The function should then sort the array according to the date property of the objects.Exampleconst arr = [ {date: "2016-06-08 18:10:00"}, {date: "2016-04-26 20:01:00"}, {date: "2017-02-06 14:38:00"}, {date: "2017-01-18 17:30:21"}, {date: "2017-01-18 17:24:00"} ]; const sortByTime = (arr ... Read More
To display characters inside a base R plot we can simply use text function with expression and if we want to display an asterisk then we need to put the asterisk within double quotes. For example, if we want to display three stars then only expression(paste("***"))) should be used. Check out the below examples to understand how it works.Example1> plot(1:10,type="n") > text(8,9,expression(paste(Sig.^"***")))OutputExample2> plot(1:10,type="n") > text(5,6,expression(paste(Less_Sig.^"**")))OutputExample3> plot(1:10,type="n") > text(2,3,expression(paste(Very_Less_Sig.^"**")))Output
There are multiple ways to create a bar plot in R and one such way is using stat_summary of ggplot2 package. In this function, we need to supply a function for the y-axis and to create the bars we must use geom="bar". The main thing is to decide which function should be used for y-axis values.ExampleConsider the below data frame:Live Demo> x y df dfOutput x y 1 Female 3 2 Male 3 3 Female 7 4 Male 3 5 Female 8 6 Female 5 7 Male 11 8 Male 6 9 Male 5 ... Read More
Suppose, we have the following array of objects −const arr = [ { "date" : "2010-01-01", "price" : 30 }, { "date" : "2010-02-01", "price" : 40 }, { "date" : "2010-03-01", "price" : 50 }, { "date" : "2010-01-01", "price2" : 45 }, { "date" : "2010-05-01", "price2" : 40 }, { "date" : "2010-10-01", ... Read More
A heatmap is a diagrammatic representation of data where the values are represented with colours. Mostly, it is used to display data that has slight variation and applied on matrix data. We can draw it for a full matrix, an upper triangular matrix as well as a lower triangular matrix. This can be done with the help of image function.Example1Live Demo> M MOutput [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 7 5 4 4 3 6 5 8 3 5 [2,] 8 7 3 5 7 4 5 2 6 6 [3,] 3 2 4 2 5 12 7 3 10 2 [4,] 5 3 6 9 5 9 2 4 5 8 [5,] 3 8 5 5 4 4 4 1 2 5 [6,] 2 3 2 4 7 8 5 8 4 4 [7,] 5 6 4 4 7 3 4 8 8 2 [8,] 4 5 2 10 5 3 5 4 6 7 [9,] 8 6 4 1 4 11 6 4 6 6 [10,] 9 5 5 4 6 2 7 3 6 5Example> image(M)Output:Example2Live Demo> M1 M1Output [,1] [,2] [,3] [,4] [,5] [,6] [1,] 24.75339 25.40680 23.76650 26.47724 24.54639 25.79895 [2,] 24.08571 25.17951 25.03599 25.63532 23.45812 25.39614 [3,] 24.53005 25.77095 26.21571 24.44029 24.69933 25.62839 [4,] 22.91202 25.49497 24.86587 25.25701 23.16166 24.34106 [5,] 25.37322 24.15308 25.58580 23.52173 25.25538 25.10577 [6,] 24.39613 26.06243 26.56054 25.19265 26.54187 24.35313Example> image(M1)Output: