Check if an array contains only one distinct element | Function to find if the array contains only one distinct element ; Create a set ; Traversing the array ; Compare and print the result ; Driver code ; Function call
function uniqueElement ( arr , n ) { var set = new Set ( ) ; for ( var i = 0 ; i < n ; i ++ ) { set . add ( arr [ i ] ) ; } if ( set . size == 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var arr = [ 9 , 9 , 9 , 9 , 9 , 9 , 9 ] ; var n = arr . length ; uniqueElement ( arr , n ) ;