- Home /
Find Highest Value in Array
Hello,
I have an array that stores a bunch of int. What I want to do, is to print the element with the highest int.
How would I go about doing that?
var turretArray : int [];
using System.Linq;
int maxValue = myArray.$$anonymous$$ax();
int maxIndex = myArray.ToList().IndexOf(maxValue);
Answer by Eric5h5 · Jun 21, 2011 at 05:47 PM
Make a function that loops through the array and stores the highest value:
function Start () {
var foo = [3, 6, 8, 33, 1, 10];
Debug.Log (MaxValue(foo));
}
function MaxValue (intArray : int[]) : int {
var max = intArray[0];
for (i = 1; i < intArray.Length; i++) {
if (intArray[i] > max) {
max = intArray[i];
}
}
return max;
}
This works well, but how do I pull out where the highest int sits in the array? EG: element [2]?
You could make a class that stores 2 ints:
class Int2 {
var value : int;
var location : int;
function Int2 (value : int, location : int) {
this.value = value;
this.location = location;
}
}
function Start () {
var foo = [3, 6, 8, 33, 1, 10];
var max = $$anonymous$$axValue(foo);
Debug.Log (max.value + " " + max.location);
}
function $$anonymous$$axValue (intArray : int[]) : Int2 {
var max = intArray[0];
var location = 0;
for (i = 1; i < intArray.Length; i++) {
if (intArray[i] > max) {
max = intArray[i];
location = i;
}
}
return Int2(max, location);
}
Using eric's code:
function Start () {
var foo = [3, 6, 8, 33, 1, 10];
Debug.Log ("The position is: " +$$anonymous$$axPos(foo));
Debug.Log ("The value is: " +foo[$$anonymous$$axPos(foo)]);
}
function $$anonymous$$axPos (intArray : int[]) : int {
var max : int = 0;
for (i = 1; i < intArray.Length; i++) {
if (intArray[i] > max) {
max = i;
}
}
return max;
}
Answer by Eric5h5 · Jun 21, 2011 at 05:14 PM
In C# you can use System.Linq along with Max():
int[] foo = {3, 6, 8, 33, 1, 10};
Debug.Log (foo.Max());
@almo: I wish you hadn't deleted your answer...the question is using JS, and you provided a reasonable answer using JS (I think it was you). I only added this as an alternative because you already answered the question appropriately; my answer won't work using JS....
Answer by Chris D · Jun 21, 2011 at 05:18 PM
You would iterate through the array, keeping the current high value in an outside variable.
var highest: int = -999;
var turretArray : int [];
//populate array
foreach (int i in turretArray){
if (turretArray[i] > highest)
highest = turretArray[i];
}
or somesuch. I don't use C# on a regular basis, but that's the gist of it.
Answer by RuinedUnknown · Oct 10, 2011 at 04:49 AM
var IntArray : int[] = [0, 5, 10, 2, 32, 26, 54, 58, 90, 22, 34, 55];
function FindMaxValue()
{
var Max : int;
//Declaring Variables Without Defining Type(Ex. var EX = 0) Compiles Faster For
//Some Reason, if You Wanted A Float Value, var EX = 0.0, an int array would be
//var IntArray = [0];
for(var i = 0; i < IntArray.Length)
{
Max = IntArray[0];
if(IntArray[i] > Max)
{
Max = IntArray[i];
}
}
print("Max Value: "+Max);
}
Your answer
Follow this Question
Related Questions
How do I sort floats by highest to lowest value? 1 Answer
Help with sorting values from a class 2 Answers
get int based on 3 ints 0 Answers
List.Sort with IComparer 2 Answers