max of array in JavaScript [duplicate] - javascript

This question already has answers here:
Find the min/max element of an array in JavaScript
(58 answers)
Closed 5 years ago.
Given an array of values [1,2,0,-5,8,3], how can I get the maximum value in JavaScript?
I know I can write a loop and keep track of the maximum value, but am looking for a more code-efficient way of doing so with the JavaScript syntax.

You can use Math.max and ES6 spread syntax (...):
let array = [1, 2, 0, -5, 8, 3];
console.log(Math.max(...array)); //=> 8

You can use the following:
yourArray.reduce((max, value) => {return Math.max(max, value)});
The reduce will itterate over values in the array, at each time returning the maximum of all of the values before the current one.

Related

How to fill multidimensional array with empty arrays [duplicate]

This question already has answers here:
Unexpected behavior using Array Map on an Array Initialized with Array Fill [duplicate]
(1 answer)
Array.fill(Array) creates copies by references not by value [duplicate]
(3 answers)
Closed 25 days ago.
I am trying to initialize a two-dimensional array with empty arrays so I can add elements to them in a larger composition using Array.push. However, when I add to the inner arrays, they all get added to. Here is a simple example:
const arr = Array(3).fill([]);
arr[0].push(42);
Now arr is [[42],[42],[42]] but I was hoping for [[42],[],[]].
I think the problem is Array.fill is putting the same referenced empty array into each slot. How do I get fill to make a distinct empty array at each slot?
You can use Array#map.
const arr = [...Array(3)].map(_ => []);
arr[0].push(42);
console.log(arr);
Or Array.from.
const arr = Array.from({length: 3}, _ => []);
arr[0].push(42);
console.log(arr);

Sort duplicate numbers from array in Javascript [duplicate]

This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
Closed 10 months ago.
I had got a question in my exam as follow:
we need to create function that will receive 1 array of all positive number and return all the duplicate values of the array in sorted order.
Here is the solution that I had implemented:
function solution(arr) {
return arr.filter((value,index)=>arr.indexOf(value)!==index).sort()
}
But the code was rejected. Can someone tell me what could be more optimized solution for this problem in Javascript?
maybe because of this :
solution(['sada','sada','r',4,'r','r']) => ['r', 'r', 'sada']
or
solution([2,2,1,3,3,6,7,7,7]) => [2, 3, 7, 7]
your solution when array have same thing more than 2 times, return wrong array .

How to Clone an Array With a Removed Element? [duplicate]

This question already has answers here:
How to get subarray from array?
(5 answers)
Closed 2 years ago.
I've searched up this question, and everywhere people seem to recommend to use array.splice(). However, splice is inplace, and, for example, in my javascript console editor.
Everywhere I seem to search, people say that splice does NOT mutate the original array, but that is clearly not the case. Now, I'm sure I will find another way to do what I want, but what is the proper way to make a copy of a piece of an array without affecting the original array?
You can use slice(), see below:
let x = [1, 2, 3, 4, 5]
console.log(x);
let sliced = x.slice(0, 2);
console.log(x);
console.log(sliced);
The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included) where begin and end represent the index of items in that array. The original array will not be modified.
Make a copy of the array using the spread operator and then you can use splice or whatever.
let arr = [1, 2, 3, 4, 5];
let newArr = [...arr];
console.log(newArr);
// newArr.splice(......)

JavaScript How to declare 16 length array with default 0 value quick? [duplicate]

This question already has answers here:
Most efficient way to create a zero filled JavaScript array?
(45 answers)
Closed 3 years ago.
let array1 = Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
how to make like this array with shortest way ? is it possible to make shorter way ?
thank you for your answers.
You can use the .fill() method:
let array1 = new Array(16).fill(0);
As its name suggests, it fills the array with some desired value.

How to determine if an array includes another array in JS? [duplicate]

This question already has answers here:
Why Array.indexOf doesn't find identical looking objects
(8 answers)
Closed 3 years ago.
I'm wondering what the best way to determine the membership of one array in another array in JS.
Here's an example
let a = [];
a.push([1,2]);
a.includes([1,2]) <- evaluates to false
a.indexOf([1,2]) <- evaluates to -1
What's the deal here? Any efficient work around?
At the moment, your search array doesn't actually equal the array within your a array as they have 2 different references in memory. However, you could convert your arrays to strings, such that your search can equal another string array within your array.
To do this you could convert your inner arrays to string using .map(JSON.stringify) and then search for the string version of your array using .includes(JSON.stringify(search_arrr)).
See example below:
let a = [];
let search = [1, 2];
a.push([1,2]);
a = a.map(JSON.stringify)
console.log(a.includes(JSON.stringify(search)));

Categories

Resources