Digitally balanced numbers | C # implementation to check if a number is a digitally balanced number ; Function to check if the digits in the number is the same number of digits ; Loop to iterate over the digits of the number N ; Loop to iterate over the map ; Driver Code ; Function to check
using System ; using System . Collections . Generic ; class GFG { static bool checkSame ( int n , int b ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; while ( n != 0 ) { int r = n % b ; n = n / b ; if ( m . ContainsKey ( r ) ) { m [ r ] = m [ r ] + 1 ; } else { m . Add ( r , 1 ) ; } } int last = - 1 ; foreach ( KeyValuePair < int , int > i in m ) { if ( last != - 1 && i . Value != last ) { return false ; } else { last = i . Value ; } } return true ; } public static void Main ( String [ ] args ) { int n = 9 ; int Base = 2 ; if ( checkSame ( n , Base ) ) Console . Write ( " Yes " ) ; else Console . Write ( " NO " ) ; } }