# Greatest number in an array

**URL:** <https://forum.kirupa.com/t/greatest-number-in-an-array/260687>\
**Category:** flash\
**Created:** [May 18, 2008, 10:59pm UTC](https://forum.kirupa.com/t/greatest-number-in-an-array/260687 "2008-05-18T22:59:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcguyver82](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@mcguyver82](https://forum.kirupa.com/u/mcguyver82)\
**Post date:** [May 18, 2008, 10:59pm UTC](https://forum.kirupa.com/t/greatest-number-in-an-array/260687/1 "2008-05-18T22:59:29Z")

</div>

How would one go about finding the greatest number in an array of numbers? For example, if you had an array like this:

(4, 8, 3, 19, 6, 11)

What would be the code used to rifle through the entire array until the greatest number in the array, in this case 19, was found and then return it’s index position?

---

<div class="post-metadata">

**Author:** ![ajcates](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ajcates/32/2435_2.png) [@ajcates](https://forum.kirupa.com/u/ajcates)\
**Post date:** [May 19, 2008, 12:20am UTC](https://forum.kirupa.com/t/greatest-number-in-an-array/260687/2 "2008-05-19T00:20:42Z")

</div>

ActionScript Code:  
[LEFT][COLOR=#000000] **function** [/COLOR] biggestNumberCOLOR=#000000[/COLOR]:[COLOR=#0000FF]Number[/COLOR] [COLOR=#000000]{[/COLOR]  
bigNum = [COLOR=#000080]0[/COLOR];  
[COLOR=#0000FF]for[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000] **var** [/COLOR] i:[COLOR=#0000FF]Number[/COLOR] = [COLOR=#000080]0[/COLOR]; i \> [COLOR=#0000FF]array[/COLOR].[COLOR=#0000FF]length[/COLOR]; i++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]  
[COLOR=#0000FF]if[/COLOR][COLOR=#000000]([/COLOR]bigNum \< [COLOR=#0000FF]array[/COLOR][COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]  
bigNum = [COLOR=#000000][/COLOR]i[COLOR=#000000][/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#0000FF]return[/COLOR] bigNum;  
[COLOR=#000000]}[/COLOR]  
[/LEFT]

---

<div class="post-metadata">

**Author:** ![mcguyver82](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@mcguyver82](https://forum.kirupa.com/u/mcguyver82)\
**Post date:** [May 19, 2008, 12:22am UTC](https://forum.kirupa.com/t/greatest-number-in-an-array/260687/3 "2008-05-19T00:22:17Z")

</div>

Nevermind, easy question I guess. I figured it out pretty quickly.

```auto

var myVar = my_array[0];
     for (var i:int=0; i<my_array.length; i++){
          if(my_array* >= myVar){
               myVar = my_array*;
          }
     }
trace(myVar);

```

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [May 19, 2008, 1:04am UTC](https://forum.kirupa.com/t/greatest-number-in-an-array/260687/4 "2008-05-19T01:04:04Z")

</div>

You could also do:

```auto
var arr:Array = [12, 1, 7, 15, 3];
trace(arr.sort(Array.NUMERIC).pop()); //15

```

---

<div class="post-metadata">

**Author:** ![Iamthejuggler](https://avatars.discourse-cdn.com/v4/letter/i/ecc23a/32.png) [@Iamthejuggler](https://forum.kirupa.com/u/Iamthejuggler)\
**Post date:** [May 19, 2008, 10:47am UTC](https://forum.kirupa.com/t/greatest-number-in-an-array/260687/5 "2008-05-19T10:47:06Z")

</div>

[QUOTE=TheCanadian;2327926]You could also do:

```auto
var arr:Array = [12, 1, 7, 15, 3];
trace(arr.sort(Array.NUMERIC).pop()); //15

```

[/QUOTE]

Noice! I love efficient code. 😃

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [May 19, 2008, 11:48am UTC](https://forum.kirupa.com/t/greatest-number-in-an-array/260687/6 "2008-05-19T11:48:35Z")

</div>

> Noice! I love efficient code.

Ironically, though, sorting the array is probably going to take O(n log(n)), whereas finding the greatest element will take O(n). Of course, maybe Flash’s internal array sort is faster than using a loop in ActionScript, but I think that the conciseness is the coolest part of TheCanadian’s code.

---

<div class="post-metadata">

**Author:** ![Iamthejuggler](https://avatars.discourse-cdn.com/v4/letter/i/ecc23a/32.png) [@Iamthejuggler](https://forum.kirupa.com/u/Iamthejuggler)\
**Post date:** [May 19, 2008, 11:58am UTC](https://forum.kirupa.com/t/greatest-number-in-an-array/260687/7 "2008-05-19T11:58:43Z")

</div>

You say “potato” i say “efficient”. I meant nice and neat rather than computationally faster! I should be more careful with my word choices 🙂
