Skip to content

Instantly share code, notes, and snippets.

@bijay-shrestha
Created June 30, 2021 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bijay-shrestha/8ffb13efc3b11d0886d8960eec68a190 to your computer and use it in GitHub Desktop.
Save bijay-shrestha/8ffb13efc3b11d0886d8960eec68a190 to your computer and use it in GitHub Desktop.
/**
* * Write a function named countDigit that returns the number of times that a given digit appears in a
* * positive number.
* *
* * For example countDigit(32121, 1) would return 2 because there are two 1s in 32121.Other examples:
* * countDigit(33331, 3) returns 4
* * countDigit(33331, 6) returns 0
* * countDigit(3, 3) returns 1
* *
* * The function should return -1 if either argument is negative, so
* * countDigit(-543, 3) returns -1.
* *
* * The function signature is
* * int countDigit(int n, int digit)
*/
package com.basic.practice;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class CountDigit {
public static void main(String[] args) {
// int number = 33313;
// int digit = 3;
// int number = 33331;
// int digit = 6;
// int number = 3;
// int digit = 3;
int number = -543;
int digit = 3;
log.info("Counting digit of number {} with digit = {}, we get: {}", number, digit, countDigit(number, digit));
}
static int countDigit(int n, int digit) {
if (n < 0) {
return -1;
}
int count = 0;
int rem = 0;
for (int i = 1; i <= n; i++) {
rem = n % 10;
n = n / 10;
if (rem == digit) {
count++;
}
}
return count;
}
}
@sanjmgr
Copy link

sanjmgr commented Aug 16, 2022

How about this solution?

static int countDigit(int n, int digit) {
    if (n < 0) return -1;

    String[] split = Integer.toString(n).split("");
    int counter = 0;

    for (String item : split) {
        if (Integer.parseInt(item) == digit) counter += 1;
    }
    
    return counter;
}

@sanjmgr
Copy link

sanjmgr commented Aug 16, 2022

static int countDigit(int n, int digit) {
    int counter = 0;
    if (n < 0) return -1;
    
    while (n > 1) {
        int rem = n % 10;

        if (rem == digit) counter++;

        n /= 10;
    }

    return counter;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment