Aryan PrajapatKnowledge Contributor
How do you find min and max value in an array
How do you find min and max value in an array
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Questions | Answers | Discussions | Knowledge sharing | Communities & more.
You can use Math.min and Math.max methods on array variables to find the minimum and maximum elements within an array. Let’s create two functions to find the min and max value with in an array,
var marks = [50, 20, 70, 60, 45, 30];
function findMin(arr) {
return Math.min.apply(null, arr);
}
function findMax(arr) {
return Math.max.apply(null, arr);
}
console.log(findMin(marks));
console.log(findMax(marks));
Brute force approach: Incrementing loop by 1
We initialize two variables, max and min, with the first element.
Now we traverse array and compare each element with min and max. If X[i] < min, we update min with X[i]. …
By the end of loop, minimum and maximum elements of the array will be stored in min and max.