Count subarrays having a single distinct element that can be obtained from a given array | C # program for the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static void divisionalArrays ( int [ ] arr , int N ) { int sum = N ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } } foreach ( KeyValuePair < int , int > x in mp ) { if ( ( int ) x . Value > 1 ) { sum += ( int ) x . Value - 1 ; } } Console . WriteLine ( sum ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 3 } ; int N = arr . Length ; divisionalArrays ( arr , N ) ; } }