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

1 Answer

0 votes
import java.util.HashSet;

public class MyClass {
    static int repeated_yes_0_no_1(int n) {
        HashSet<Integer> hs = new HashSet<>();
     
        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;

        System.out.println(GetTotalNumbersWithNoRepeatedDigits(start, end));
    }
}




/*
run:

90

*/

 



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