Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.5 kB
7
Indexable
import java.util.Scanner;
import java.util.Arrays;

public class CompactNumber{
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		System.out.print("enter ur number: ");
		int number = scanner.nextInt();

		boolean can_compact = true;
		while(can_compact){
			if (compact(number) == number)
				can_compact = false;
			number = compact(number);
		}

		System.out.println(compact(number));
	}

	static int compact(int number){

		String str_num = String.valueOf(number);
		String unique_digits = "";
		String count_numbers = "";
		int counter;
		for (int i =0; i < str_num.length(); i++){
			counter = 1;
			for(int j=i+1; j < str_num.length(); j++){
				if(str_num.charAt(i) == str_num.charAt(j)){
					counter++;
					continue;
				}

			}
			
			String temp = String.valueOf(str_num.charAt(i));

			if(! unique_digits.contains(temp)){
				unique_digits += str_num.charAt(i);
				if(counter>1){
					count_numbers += counter;

				}
			}
				
		}
		
		int[] sorted = sortNumber(Integer.valueOf(unique_digits+count_numbers));
		String output = "";
		for(int digit: sorted){
			output += digit;
		}
		return Integer.valueOf(output);
	}

	static int[] sortNumber(int number){
		int[] digits = new int[String.valueOf(number).length()];
		int counter = 0;
		while(number>0){
			digits[counter] = number%10;	
			counter++;
			number /= 10;	
		}
		Arrays.sort(digits);
		return digits;

	}
}