How to get the total numbers with no repeated digits from a range of numbers in C#

1 Answer

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

public class MyClass
{
	static int repeated_yes_0_no_1(int n) {
		HashSet<int> hs = new HashSet<int>();

		while (n != 0) {
			int digit = n % 10;

			if (hs.Contains(digit)) { // repeated = yes
				return 0;
			}

			hs.Add(digit);

			n = n / 10;
		}
		return 1;
	}

	static int GetTotalNumbersWithNoRepeatedDigits(int start, int end) {
		int total = 0;

		for (int i = start; i <= end; i++) {
			total += repeated_yes_0_no_1(i);
		}

		return total;
	}
	public static void Main(string[] args)
	{
		int start = 1, end = 100;

		Console.WriteLine(GetTotalNumbersWithNoRepeatedDigits(start, end));
	}
}




/*
run:

90

*/

 



answered Jan 14, 2023 by avibootz
edited Jan 14, 2023 by avibootz
...