Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

second largest element in array

      // [10,30,15,4,2,5]
      // when you get element< smallest
             //interchange second_smallest <-> smallest
             //smallest= element
      //else if u get element< second_smallest
          //if element != smallest then
            //second_smallest= element
            
            
      //find neat code below
      
      long min=Integer.MAX_VALUE;
        long sMin= Integer.MAX_VALUE;
        
        for(int i=0; i<n; i++){
            if(a[i]< min){
                sMin=min;
                min= a[i];
            }
            else if(a[i]<sMin && a[i]!= min){
                sMin= a[i];
            }
        }
        
        
        
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #largest #element #array
ADD COMMENT
Topic
Name
5+8 =