Digit That Appears the Least Number of Times

The program must accept an integer N as the input. The task is to determine the digit that appears the least number of times in N. If there are multiple such digits, the program should print the smallest among them.

Boundary Condition(s):
1 <= N <= 10^18

Input Format:
The first line contains the integer N.

Output Format:
A single line contains the digit that appears the least number of times in N.

Example Input/Output 1:
Input:
1123451456

Output:
2

Explanation:
The digits 3, 6, 2, and 7 appear only once in the number, but 2 is the smallest among them. Hence the output is 2.

Example Input/Output 2:
Input:
9292292285

Output:
5

Explanation:
The digits 5 and 8 appear only once in the number. But 5 < 8, so the output is 5.

N = input().strip()
counts = [0] * 10

for digit in N:
    d = int(digit)
    counts[d] += 1

min_count = min([count for count in counts if count > 0])
least_digits = [str(i) for i, count in enumerate(counts) if count == min_count]

print(min(least_digits))
#include<stdio.h>
#include<string.h>

int main() {
    char N[20];
    scanf("%s", N);
    int counts[10] = {0};

    for(int i = 0; i < strlen(N); i++) {
        int d = N[i] - '0';
        counts[d]++;
    }

    int min_count = 18; // Max size + 1
    for(int i = 0; i < 10; i++) {
        if(counts[i] > 0 && counts[i] < min_count) {
            min_count = counts[i];
        }
    }
    for(int i = 0; i < 10; i++) {
        if(counts[i] == min_count) {
            printf("%d", i);
            break;
        }
    }
    return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    string N;
    cin >> N;
    int counts[10] = {0};

    for(char digit : N) {
        int d = digit - '0';
        counts[d]++;
    }

    int min_count = 19; // Max size + 1
    for(int i = 0; i < 10; i++) {
        if(counts[i] > 0 && counts[i] < min_count) {
            min_count = counts[i];
        }
    }
    for(int i = 0; i < 10; i++) {
        if(counts[i] == min_count) {
            cout << i;
            break;
        }
    }
    return 0;
}
import java.util.Scanner;

public class LeastFrequentDigit {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String N = sc.next();
        int[] counts = new int[10];

        for(char ch : N.toCharArray()) {
            int d = ch - '0';
            counts[d]++;
        }

        int min_count = 19; // Max size + 1
        for(int i = 0; i < 10; i++) {
            if(counts[i] > 0 && counts[i] < min_count) {
                min_count = counts[i];
            }
        }
        for(int i = 0; i < 10; i++) {
            if(counts[i] == min_count) {
                System.out.println(i);
                break;
            }
        }
    }
}

Leave a Reply

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

More posts. You may also be interested in.