function findHighestNumber(nums) {
let inputs = nums.filter((val, i) => nums.indexOf(val) === i)
let max = Math.max(...nums);
let min = Math.min(...nums);
return max + (-min);
}
console.log(difference([1, 7, 18, -1, -2, 9]));
Find index of Largest value in An Array in JavaScript
<!DOCTYPE html>
<html>
<body>
<p>In this example we can able to check the index value of largest array value</p>
<script>
var marks = [30, 100, 50, 90, 40];
function IndexValueOfALargestArrayValue(array){
var startingValue = 1;
var maximumValue = 0;
for(startingValue; startingValue < array.length; startingValue++){
if(array[maximumValue] < array[startingValue]){
maximumValue = startingValue;
}
}
return maximumValue;
}
document.write("The largest Number Index value is:" +IndexValueOfALargestArrayValue(marks));
</script>
</body>
</html>
//For Values
var arr =[1,10,3]
var min = Math.min.apply(null, arr),
max = Math.max.apply(null, arr);
//For Objects
var arr = [{a: 1},{a: 10},{a: 3}]
var values = arr.map(val => val.a);
var max = Math.max.apply(null, values);
console.log(max)
#include <stdio.h>
int main() {
int n;
double arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
printf("Enter number%d: ", i + 1);
scanf("%lf", &arr[i]);
}
// storing the largest number to arr[0]
for (int i = 1; i < n; ++i) {
if (arr[0] < arr[i]) {
arr[0] = arr[i];
}
}
printf("Largest element = %.2lf", arr[0]);
return 0;
}
Find largest number from array by function in javascript
function largestElements(numbers) {
var largest = 0;
for (let i = 0; i < numbers.length; i++) {
var elements = numbers[i];
if (elements > largest) {
largest = elements;
}
}
return largest;
}
const numbers = [2, 3, 4, 8, 5, 2, 4];
console.log(largestElements(numbers));
//Output: 8
#include <stdio.h>
int main() {
int n;
double arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
printf("Enter number%d: ", i + 1);
scanf("%lf", &arr[i]);
}
// storing the largest number to arr[0]
for (int i = 1; i < n; ++i) {
if (arr[0] < arr[i]) {
arr[0] = arr[i];
}
}
printf("Largest element = %.2lf", arr[0]);
return 0;
}