Create a Number from Non-Repeating Digits

The program must accept an integer N as the input. The task is to form a new number by using only the non-repeating digits in N. If no such number can be formed, print -1.

Boundary Condition(s):
0 <= N <= 10^9

Input Format:
The first line contains the integer N.

Output Format:
The first line contains the new number formed using the non-repeating digits of N or -1.

Example Input/Output 1:
Input:
122345

Output:
345

Explanation:
Digits 1 and 2 are repeating. By removing these repeating digits, we get the number 345.

Example Input/Output 2:
Input:
9999999

Output:
-1

Explanation:
All digits in the number are repeating. Hence, no number can be formed. So, the output is -1.

n = input().strip()
unique_digits = ''.join([ch for ch in n if n.count(ch) == 1])
print(unique_digits if unique_digits else -1)
#include<stdio.h>
#include<string.h>

int main() {
    char n[12];
    scanf("%s", n);
    int len = strlen(n), flag = 0;
    for(int i = 0; i < len; i++) {
        if(strchr(n + i + 1, n[i]) == NULL && strchr(n, n[i]) == n + i) {
            printf("%c", n[i]);
            flag = 1;
        }
    }
    if(!flag) printf("-1");
    return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    string n;
    cin >> n;
    bool found = false;
    for(char ch : n) {
        if(count(n.begin(), n.end(), ch) == 1) {
            cout << ch;
            found = true;
        }
    }
    if(!found) cout << "-1";
    return 0;
}
import java.util.Scanner;

public class NonRepeatingDigits {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n = sc.next();
        boolean found = false;
        for(char ch : n.toCharArray()) {
            if(n.indexOf(ch) == n.lastIndexOf(ch)) {
                System.out.print(ch);
                found = true;
            }
        }
        if(!found) System.out.println("-1");
    }
}
Previous Article

Common Digits in Two Numbers

Next Article

Count of Digits Inverted in Their Position

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *