Check if an array contains only one distinct element | Function to find if the array contains only one distinct element ; Assume first element to be the unique element ; Traversing the array ; If current element is not equal to X then break the loop and print No ; Compare and print the result ; Driver code ; Function call
function uniqueElement ( arr ) { var x = arr [ 0 ] ; var flag = 1 ; for ( var i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] != x ) { flag = 0 ; break ; } } if ( flag == 1 ) document . write ( " " ) ; else document . write ( " " ) ; } var arr = [ 9 , 9 , 9 , 9 , 9 , 9 , 9 ] ; uniqueElement ( arr ) ;