Maximum area rectangle by picking four sides from array | Function for finding max area ; Traverse through array ; If this is first occurrence of arr [ i ] , simply insert and continue ; If this is second ( or more ) occurrence , update first and second maximum values . ; Return the product of dimensions ; Driver Code
function findArea ( arr , n ) { let s = new Set ( ) ; let first = 0 , second = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! s . has ( arr [ i ] ) ) { s . add ( arr [ i ] ) ; continue ; } if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) second = arr [ i ] ; } return ( first * second ) ; } let arr = [ 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 ] ; let n = arr . length ; document . write ( findArea ( arr , n ) ) ;