Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the highest number in an array? [duplicate]

Tags:

Possible Duplicate:
How to sort an array in BASH

I have numbers in the array 10 30 44 44 69 12 11.... How to display the highest from array?

echo $NUM //result 69 
like image 229
Charlie Avatar asked Oct 05 '12 10:10

Charlie


People also ask

How do you find the greatest number in an array?

To find the largest element from the array, a simple way is to arrange the elements in ascending order. After sorting, the first element will represent the smallest element, the next element will be the second smallest, and going on, the last element will be the largest element of the array.

Can you find which value occurs the maximum number of times?

Mode is the highest occurring figure in a series. It is the value in a series of observation that repeats maximum number of times and which represents the whole series as most of the values in the series revolves around this value. Therefore, mode is the value that occurs the most frequent times in a series.


1 Answers

You can use sort to find out.

#! /bin/bash ar=(10 30 44 44 69 12 11) IFS=$'\n' echo "${ar[*]}" | sort -nr | head -n1 

Alternatively, search for the maximum yourself:

max=${ar[0]} for n in "${ar[@]}" ; do     ((n > max)) && max=$n done echo $max 
like image 53
choroba Avatar answered Oct 02 '22 01:10

choroba