Introduction
Return Largest Numbers in Array is a coding challenge that can be solved in any programming language, but for this tutorial, we will use JavaScript to solve it. In this challenge, we will be given a large array with multiple sub-arrays [[1,3,4], [3,11,2], [4,3,12]], and each sub-array contains numerous numeric values. We have to find the largest value from each sub-array and return these largest values in a single array. I know it sounds confusing, but don't worry, you'll understand it as we dive into the solution. Let's break this problem into smaller pieces for better understanding.
Remember, there are countless ways to solve a single coding challenge or problem in every programming language. In this article, I will emphasize one of them.
Examples:
let array = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]];
Have you seen the biggest array above? Now, let's understand how to solve this. You can see there are four sub-arrays within this.
An array [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]];
[[5], [27], [39], [1001]];
Have you seen above, we have four values: 5, 27, 30, and 1001. The biggest value in the first array is 5, in the second array it is 27, in the third array it is 30, and in the fourth array it is 1001.
Now, we need to omit the array brackets and add only the numbers, as shown in the example below:
[5, 27, 39, 1001];
Solution:
This is our final solution. I hope you understood the problem. It is crucial to have complete knowledge about the problem that you are going to solve. Now, let's start coding and solve it using JavaScript.
function largestOfFour(arr) {
let finalArr = [];
for(let i = 0; i < arr.length; i++) {
finalArr.push(arr[i].sort((a, b) => b - a)[0])
}
return finalArr;
}
console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));
Don’t worry, I will explain all the code I used above using the breaking principle. Let’s start.
This is a function that you will use to solve this problem. I won’t go into depth on this since it is a standard function. You need to start your solution within this.
function largestOfFour(arr) {
return arr;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
As mentioned earlier, I declare a variable named finalArr and initialize it as an empty array []. I will then push the final output into this array.
After that, I started a for loop to access the array so that we could work with sub-arrays for our desired output. Within this loop, I pushed all the values into finalArr using the push method of JavaScript. Since we only need to return the largest value of each sub-array, we used sort((a, b) => b - a) to sort the elements. Lastly, I used [0] to access the first value of each sub-array.
Note: Using sort((a, b) => b - a) will sort all elements of the array in descending order, such as [3, 2, 1], from the largest element to the smallest.
for(let i = 0; i < arr.length; i++) {
finalArr.push(arr[i].sort((a, b) => b - a)[0])
}
By using finalArr.push(arr[i]), the output will be like this:
[[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]];
After using the sort method (finalArr.push(arr[i].sort((a, b) => b - a))), the output will be like this:
[[5, 4, 3, 1], [27, 26, 18, 13], [39, 37, 35, 32], [1001, 1000, 857, 1]];
As seen above, all the sub-arrays have been sorted in descending order. By using [0], we can obtain the first value of each sub-array, like this:
[5, 27, 39, 1001];