How to check if a number is a repdigit number (a natural number composed of repeated digits) in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

internal class Program
{
	static bool isRepdigitNumber(int num) {
	    string str = num.ToString();
        char[] arr = str.ToCharArray();
		
		var hset = new HashSet<char>(arr);

        return hset.Count == 1;
    }
    
	public static void Main(string[] args)
	{
		int num = 8888888;
   
        if (isRepdigitNumber(num)) {
            Console.WriteLine("yes");
        } else {
            Console.WriteLine("no");
        }
	}
}

  
  
/*
run:
  
yes
  
*/

 

 



answered Feb 7, 2024 by avibootz
...