Second highest element in an array


/ Published in: ActionScript 3
Save to your folder(s)

Looking forward for the most optimized solution for the same


Copy this code and paste it in your HTML
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
  3. <mx:Script>
  4. <![CDATA[
  5. // second highest element in an array
  6. public var arr:Array = new Array("23", "12", "45", "22", "42", "87", "97", "13")
  7. public function init():void{
  8. arr.sort();
  9. txt.text = arr[arr.length - 2] // 87
  10. for(var i=0;i<arr.length;i++){
  11. txt.text += "\n "+arr[i];
  12. }
  13. }
  14. ]]>
  15. </mx:Script>
  16. <mx:TextArea x="118" y="57" width="553" height="412" id="txt"/>
  17. </mx:Application>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.