Count subarrays having a single distinct element that can be obtained from a given array | Function to count subarrays of single distinct element into which given array can be split ; Stores the count ; Stores frequency of array elements ; Traverse the array ; Traverse the map ; Increase count of subarrays by ( frequency - 1 ) ; Driver Code
function divisionalArrays ( arr , N ) { var sum = N ; var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . set ( arr [ i ] , 1 ) ; } } mp . forEach ( ( value , key ) => { if ( value > 1 ) { sum += value - 1 ; } } ) ; document . write ( sum ) ; } var arr = [ 1 , 1 , 3 ] ; var N = arr . length ; divisionalArrays ( arr , N ) ;