In order to solve this problem I am going to use JavaScript. This problem is going to find the second largest element inside an array.
First we declare a variable named: "arr" which is going to contain the values we are going to compare.
let arr = [ 8, 6, 4, 12, 10, 2 ];
Then we declare the function "secondLargest" passing the array "arr" as a paramether.
let arr = [ 8, 6, 4, 12, 10, 2 ];
function secondLargest(arr) {
}
function secondLargest(arr) {
}
We can start off by declaring two variables largest and seclargest, and initialize then to zero.
let arr = [ 8, 6, 4, 12, 10, 2 ];
function secondLargest(arr) {
let largest = 0;
let seclargest = 0;
}
function secondLargest(arr) {
let largest = 0;
let seclargest = 0;
}
Now we should compare all the elements within the array to figure out the second largest. In order to do that we need to first find out the largest element. We are going to use a for loop to iterate through the array and compare the values with a if statement.
let arr = [ 8, 6, 4, 12, 10, 2 ];
function secondLargest(arr) {
let largest = 0;
let seclargest = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
}
function secondLargest(arr) {
let largest = 0;
let seclargest = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
}
In the code above, "if (arr[i] > largest)" is going to compare whether the value of "arr[i]"is greater than the value of the "largest" variable. When the for loop compares the value "8" for the first time "8" is larger than "0" which would be the current value of the "largest" variable; therefore, the value of the variable "largest" now is going to be equals to "8".
When the for loop compares the second value of "6" then it would ask: 6 > 8, being the result false, then we should say that 8 is still the largest value and 6 will become the second largest value. In order to accomplish this we add an else statement and utilize the variable "seclargest".
let arr = [ 8, 6, 4, 12, 10, 2 ];
function secondLargest(arr) {
let largest = 0;
let seclargest = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > largest) {
largest = arr[i];
} else {
if (arr[i] > seclargest) {
seclargest= arr[i];
}
}
}
}
function secondLargest(arr) {
let largest = 0;
let seclargest = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > largest) {
largest = arr[i];
} else {
if (arr[i] > seclargest) {
seclargest= arr[i];
}
}
}
}
Now, when the for loop compares the value of "4" then it would ask: 4 > 8, (8 still is the greatest value) this would also be false, therefore the code will jump to the else statement and ask again: 4 > 6, (6 still being the second largest value), this will also be false; therefore, the value of both variable will continue to be the same.
When comparing 12 > 8, this will be true, now the value of the "largest" variable is equals to 12. What would happen if the value 8 which now would be the second largest number? We can solve this simply by making the "secLargest" variable equals to the former "largest" value.
let arr = [ 8, 6, 4, 12, 10, 2 ];
function secondLargest(arr) {
let largest = 0;
let seclargest = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > largest) {
seclargest = largest;
largest = arr[i];
} else {
if (arr[i] > seclargest) {
seclargest= arr[i];
}
}
}
}
largest = arr[i];
} else {
if (arr[i] > seclargest) {
seclargest= arr[i];
}
}
}
}
Finally we print the final value of the "secLargest" variable and call the function.
let arr = [ 8, 6, 4, 12, 10, 2 ];
function secondLargest(arr) {
let largest = 0;
let seclargest = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > largest) {
seclargest = largest;
largest = arr[i];
}
else {
if (arr[i] > seclargest) {
seclargest = arr[i];
}
}
}
console.log(largest);
console.log(seclargest);
}
secondLargest(arr);
function secondLargest(arr) {
let largest = 0;
let seclargest = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > largest) {
seclargest = largest;
largest = arr[i];
}
else {
if (arr[i] > seclargest) {
seclargest = arr[i];
}
}
}
console.log(largest);
console.log(seclargest);
}
secondLargest(arr);
The second largest value here would be 10.
0 Comments:
Post a Comment