Skip to main content

Posts

Showing posts with the label largest

Find the largest and smallest number in an unsorted integer array or list

 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class LargestSmallest{ public static void main (String []args){ int [] arr = { 2 , 3 , 6 , 3 , 3 , 3 , 4 , 1 }; int large = Integer. MIN_VALUE ; int small = Integer. MAX_VALUE ; for ( int i= 0 ; i<arr. length ; i++){ if (arr[i]<small) small = arr[i]; else if (arr[i]>large) large = arr[i]; } System. out . println ( "largest:" +large); System. out . println ( "smallest:" +small); } }  Java 2nd Approach 1 2 3 4 5 6 7 8 9 import java.util.Arrays; public class LargestSmallest2{ public static void main (String []args){ int [] arr = { 2 , 3 , 6 , 3 , 3 , ...