Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by oliver-jones · Jun 21, 2011 at 05:00 PM · arrayintsorthighest

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 [];
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Seyed_Morteza_Kamaly · Jul 03, 2018 at 05:47 AM 0
Share
 using System.Linq;
 
 int maxValue = myArray.$$anonymous$$ax();
 int maxIndex = myArray.ToList().IndexOf(maxValue);

5 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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;
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oliver-jones · Jun 21, 2011 at 06:21 PM 0
Share

This works well, but how do I pull out where the highest int sits in the array? EG: element [2]?

avatar image Eric5h5 · Jun 21, 2011 at 06:35 PM 1
Share

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);
 }
avatar image Chris D · Jun 21, 2011 at 06:37 PM 0
Share

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;
 }
avatar image
7

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());
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chris D · Jun 21, 2011 at 05:19 PM 0
Share

This is a much more elegant solution.

avatar image almo · Jun 21, 2011 at 05:20 PM 0
Share

Do this.

avatar image oliver-jones · Jun 21, 2011 at 05:27 PM 0
Share

This is C# though, I am working with JS

avatar image Eric5h5 · Jun 21, 2011 at 05:30 PM 0
Share

@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....

avatar image
4

Answer by inewland · Oct 21, 2014 at 07:54 PM

 var max = Mathf.Max(myArray);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Sorting through an array of gameobjects 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges