Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

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 admin.cs.miu.edu #
 
PREVIOUS NEXT
Tagged: #largest #element #array
ADD COMMENT
Topic
Name
2+1 =