We are required to write a JavaScript function that takes in an array of literal values. The array might contain some repeating values inside it.Our function should remove all the repeating values keeping the first instance of repeating value in the array.ExampleThe code for this will be −const arr = [1, 5, 7, 4, 1, 4, 4, 6, 4, 5, 8, 8]; const deleteDuplicate = (arr = []) => { for(let i = 0; i < arr.length; ){ const el = arr[i]; if(i !== arr.lastIndexOf(el)){ arr.splice(i, 1); } else{ i++; }; }; }; deleteDuplicate(arr); console.log(arr);OutputAnd the output in the console will be −[ 7, 1, 6, 4, 5, 8 ]
The replacement of values in a vector with the values in the same vector can be done with the help of replace function. The replace function will use the index of the value that needs to be replaced and the index of the value that needs to be placed but the output will be the value in the vector.Example1Live Demo> x1 x1Output[1] 3 0 1 0 1 1 1 1 2 1Example> replace(x1, c(10), x1[c(1)])Output[1] 3 0 1 0 1 1 1 1 2 3 Example2Live Demo> x2 x2Output[1] 5 1 4 2 3 4 2 4 5 3 6 ... Read More
Suppose we have two arrays of objects, the first of which contains some objects with user ids and user names.The array contains objects with user ids and user addresses.The array is −const arr1 = [ {"id":"123", "name":"name 1"}, {"id":"456", "name":"name 2"} ]; const arr2 = [ {"id":"123", "address":"address 1"}, {"id":"456", "address":"address 2"} ];We are required to write a JavaScript function that takes in two such arrays and merges these two arrays to form a third array.The third array should contain the user id, name, and address object of corresponding users.ExampleThe code for this will be −const ... Read More
The standardization means converting a vector or column of an R data frame in a way such that the mean of the same becomes 0 and the standard deviation becomes 1, that is it should be converted to standard normal distribution. In R, it can be easily done with the help of scale function. Check out the below example to understand how it is done.ExampleConsider the below data frame:Live Demo> set.seed(3665) > x1 x2 x3 x4 x5 x6 df dfOutputx1 x2 x3 x4 x5 x6 1 1.3958185 49.39843 128.5224 3 4.183664 2.33406246 2 1.0467979 48.90103 120.5796 7 3.526731 0.02043217 3 ... Read More
We are required to write a JavaScript function that takes in an array of literal values. Our function should pick all those values from the array that appears exactly twice in the array and return a new array of those elements.ExampleThe code for this will be −const arr = [0, 1, 2, 2, 3, 3, 5]; const findAppearances = (arr, num) => { let count = 0; for(let i = 0; i < arr.length; i++){ const el = arr[i]; if(num !== el){ continue; }; ... Read More
The rgb colors are referred to red green and blue. This combination helps us to create many different colors. In R, we can use rgb function to create a plot using with different colors along with the image function. If we want to have a plot with rgb colors without any axes title or axes labels then the appropriate arguments should be used inside the image function as shown in the below example.ExampleConsider the below data frame:Live Demo> set.seed(9991) > x1 x2 x3 df dfOutput x1 x2 ... Read More
Suppose, we have two different array of objects that contains information about the questions answered by some people −const arr1=[ { PersonalID: '11', qusetionNumber: '1', value: 'Something' }, { PersonalID: '12', qusetionNumber: '2', value: 'whatever' }, { PersonalID: '13', qusetionNumber: '3', value: 'anything' }, { PersonalID: '14', qusetionNumber: '4', value: 'null' } ]; const arr2=[ { qusetionNumber: '2', chID: '111', cValue: 'red' }, { qusetionNumber: '2', chID: '112', cValue: 'green'}, { qusetionNumber: '2', chID: '113', cValue: 'blue' }, {qusetionNumber: '3', choiceID: '114', cValue: 'yellow'}, {qusetionNumber: '4', choiceID: '115', cValue: 'red'} ];We ... Read More
To create a random vector that sums to 1, we can use uniform distribution. The main thing that needs to be done cautiously is we should include 0 in the vector with randomly generating uniform distribution values. Check out the below examples to understand how it can be done.Example1Live Demo> x1 x1Output[1] 0.45490995 0.23826247 -0.07338489 -0.33361362 0.26125094 -0.45243689 [7] 0.05967125 0.43007076 0.04069027 0.37457976Example> sum(x1)Output[1] 1Example2Live Demo> x2 x2Output[1] 1.84330339 -0.11622911 -0.15001654 0.07803346 -0.17353612 0.23651847 [7] -0.21121933 -0.30938763 0.44503222 -0.64249881Example> sum(x2)Output[1] 1Example3Live Demo> x3 x3Output[1] 2.63249755 1.17230387 -0.28068787 0.58040911 -1.48530836 -0.04894802 [7] 0.66718009 0.13504265 -0.18253891 -0.49757615 1.63580429 -2.31002917 [13] 2.66256899 -2.40636756 ... Read More
We sometimes want to highlight the main title of a plot and one of the ways to do it is changing the font of the title to a unique or using a mixed font for the title. If we want to used mixed font then we need to use the appropriate font style for the title inside as shown in the below examples.Example1> plot(rpois(10,5),main=substitute(paste(italic("Point Chart"),": Poisson Distribution")))Output:Example2> plot(rpois(10,2),main=substitute(paste(bold("Point Chart"),": Poisson Distribution")))Output:
We are required to write a JavaScript function that takes in a multidimensional array of arrays of literal values. Our function should return the intersecting array of all the subarrays present in the input array.ExampleThe code for this will be −const arr = [ ["garden","canons","philips","universal"], ["universal","ola","uber","bangalore"] ]; const findMultiIntersection = (arr = []) => { const res = []; arr.forEach(el => { const thisObj = this; el.forEach(element => { if(!thisObj[element]){ thisObj[element] = true; } else{ res.push(element) }; }); }, {}); return res; }; console.log(findMultiIntersection(arr));OutputAnd the output in the console will be −[ 'universal' ]
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP