-3

Given a non-negative int n, how do i return the count of the occurrences of a digit e.g 7, so for example 717 yields 2? (no loops). Here is my code but it doesn't work well.

 public int count7(int n) {
int count = 0;
  if(n==7){
  count++;

  return count;
  }
 else if(n>7 && n<100)

  return count7(n/10)+count7(n%10);

  else if( n>100)

  return count7(n/10)+count7(n%10);

  else return 0;

}
sully11
  • 37
  • 1
  • 9
  • to count the number of 7, you have to look at every number to see if that number is a 7. That implies a loop of some sort. Also, what exactly do you mean by `doesn't work well`? – njzk2 Dec 17 '14 at 16:15
  • i am trying to solve it recursively. It only works with smaller integers like e.g 7123 but not with bigger one's like e.g 777576197. I will ask on math overflow then – sully11 Dec 17 '14 at 16:22
  • your problem with this code is not with large number, it is with number containing `9` or `8`, because of `count7(n%10)`. – njzk2 Dec 17 '14 at 16:36

4 Answers4

3

Your code seems like it should be working. Not sure what you mean by "doesn't work well". Here is an a bit cleaner/shorter version of the same solution:

int count7(int n) {
    if(n == 0) return 0;
    return (n%10 == 7 ? 1 : 0) + count7(n/10);
}
Dima
  • 39,570
  • 6
  • 44
  • 70
  • thanks, it works perfectly. Mine wasn't working for bigger values. I was getting a stackoverflow error. – sully11 Dec 17 '14 at 16:29
  • Ah, I see. It's not because of "bigger values", it's because of this piece: `else if(n>7 && n<100) return count7(n/10)+count7(n%10);`. Suppose, `n == 8`. Then `n%10` is also 8, and you will keep recursing infinitely. So, you'll get this error with any number, containing 8 or 9, not necessarily a large one. Even just `8` will do. Replace `7` with `9` in the condition if you want to fix it. – Dima Dec 17 '14 at 16:49
1

For the fun of it:

  public static int count7( int n ) {
    return Integer.toString( n )
                  .replaceAll( "[^7]" , "" )
                  .length();
  }

Probably better fits to code golf ,-)

Sirko
  • 72,589
  • 19
  • 149
  • 183
0

A fast solution :

return Integer.toString(n).split("7").length-1;

When you want to look at the digits in the decimal representation of a number, it's usually reasonable to let the already available and optimized number stringification function do the job (that is Integer.toString(yournumber)). Of course there are loops behind, but there's even loops in the implementation of your recursive calls...

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Doesn't work for me when the digit starts or ends with the integer you are splitting on. `"725672".split("7")` returns `["256","2"]`. `length-1` will return `1`. The answer is 2. – Chielt Sep 21 '17 at 09:45
0

Here is a solution :

public int count(int number, int digit){
    String numberToString = new Integer(number).toString();
    String digitToString = new Integer(digit).toString();
    return StringUtils.countMatches(numberToString,digitToString);
}

It will count for you how many digit are in number

So count(717,7) will return 2

Hichamov
  • 636
  • 6
  • 8