Absolute distinct count in a sorted array | The function returns number of distinct absolute values among the elements of the array ; Note that set keeps only one copy even if we try to insert multiple values ; Driver code
function distinctCount ( arr , n ) { let s = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) s . add ( Math . abs ( arr [ i ] ) ) ; return s . size ; } let arr = [ - 2 , - 1 , 0 , 1 , 1 ] ; let n = arr . length ; document . write ( " " + distinctCount ( arr , n ) ) ;