- Home /
Comparing value of multiple ints and returning the largest.
I'm going to have a series many integers, and after a while, I want to get all their values, and return the largest int in the series, then perform an action based on the actual variable returned (not its value).
Ex. Suppose we took a bunch of ints that represented foods, and after comparing the amount of each food present,apple has the most and is returned as 'apple' (or maybe its index if using an array), not the amount of apples.
I was thinking of using an array and an enum, but I'm not sure of the easiest and least redundant approach to doing this.
Answer by Wolfram · Jan 10, 2013 at 07:53 PM
Today is your lucky day, I was in the mood to provide a script ;-)
using UnityEngine;
using System.Collections;
public class FruitClass : MonoBehaviour {
public enum Fruits {
Apple,
Orange,
Banana,
numEntries // we use this as marker for the number of entries
}
public int[] myFruits;
void Awake(){
// provide necessary space
myFruits=new int[(int)Fruits.numEntries];
// fill array
myFruits[(int)Fruits.Apple]=3;
myFruits[(int)Fruits.Orange]=1;
myFruits[(int)Fruits.Banana]=4;
// find index of maximum value
int maxIndex=-1;
int maxValue=0;
for(int i=0;i<(int)Fruits.numEntries;i++){
if(myFruits[i]>maxValue){
maxValue=myFruits[i];
maxIndex=i;
}
}
// output result
if(maxIndex>=0)
Debug.Log("The fruit I have most of are "+(Fruits)maxIndex+"s, of which there are "+maxValue+".");
}
}
The int<->Fruits casts cannot be prevented with this approach in C#, it forces a compiler error when missing.
EDIT: Look! I even documented it! :-D
Thank you so much. I'm going to try this as soon as I get in front of my workstation!
Answer by $$anonymous$$ · Jan 10, 2013 at 11:06 PM
Or you can use System.Collections.Generic.Dictionary
, where your keys are from the Fruit enum, the values are integers.
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx
For getting the min/max items, you can use LINQ, like described here:
http://stackoverflow.com/questions/3653970/minimum-value-in-dictionary-using-linq
This is a simple and elegant solution.
Answer by Landern · Jan 10, 2013 at 01:44 PM
The easiest way of doing it using a standard array(square brackets).
// c#
int[] nums = { 1, 2, 3, 4, 5, 6, 7 };
nums.Max(); // Will result in 7
nums.Min(); // Will result in 1
// US/JS
var nums: int[] = { 1, 2, 3, 4, 5, 6, 7 };
nums.Max(); // Will result in 7
nums.Min(); // Will result in 1
Ah, so in the declaration of int[] nums, could I do something like int[] nums = { apple = 3, orange = 1, banana = 4} and expect to have the $$anonymous$$ be orange and the max be banana? Would I need to declare the fruit outside of the int[] declaration? Lastly, is $$anonymous$$ and max returning the index or the actual largest value?
you could not, in this case i would create a custom class(i'm not sure if you are using c# or unity/javascript) that used an enum type for say Fruit property and a property of type int that was i assume the Amount.
It is returning the max or $$anonymous$$ because this is an int type, it will find the highest or lowest value in the array and return a single int.
@z_murc: I think that comment deserves to be posted as a separate answer.
With the drawback in $$anonymous$$d you always have when using Linq: it's very elegant and can accomplish a lot with only one or two lines of code - but if you need performance, you need to be careful and know exactly what's going on, otherwise it can become very expensive very quickly.
@Wolfram : converted it. You can put this comment under it. Indeed you have to be careful with it, but in this case (some fruits and their amounts) I don't tink it poses an issue.
Your answer
Follow this Question
Related Questions
Comparing Two Arrays 1 Answer
Trying to sort an array. What's wrong here? 1 Answer
Sorting array 0 Answers
What to use to store data like eg. block types? 1 Answer
List Sorting But Random Order 2 Answers