5
\$\begingroup\$

Approach 1:

Repeated division-modulus operations:

long num = 123456789; int count = 0; while(num > 0) { int digit = num % 10; if(digit == 1) count ++; num /= 10; } 

Approach 2:

Convert it into an String and get the characters at the position:

long num = 123456789; int count = 0; String s = String.valueOf(num); for(int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if(ch == '1') count ++; } 

The second operation doesn't need to get the remainder and the quotient each time. A charAt() method can be enough.

Which approach is considered to be better and why?

Consider taking the input from the console.

1st Case:

long num = scanner.nextLong(); 

2nd Case:

String s = scanner.nextLine(); 

Here there would be no overhead on converting the number to string.

Also let us assume it is for positive numbers.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Welcome to CR! In what context will this be used for? \$\endgroup\$ Commented Jul 23, 2015 at 6:38
  • \$\begingroup\$ Knowledge gaining context! \$\endgroup\$ Commented Jul 23, 2015 at 6:39
  • \$\begingroup\$ As in you're just interested to count the number of 1s? Would this be used in a method with the signature private static int countOccurrence(int number, int searchFor)? \$\endgroup\$ Commented Jul 23, 2015 at 6:43
  • \$\begingroup\$ @h.j.k. Counting numbers is just an example. I wanted to know which would be better to get the individual digits. \$\endgroup\$ Commented Jul 23, 2015 at 6:44

4 Answers 4

1
\$\begingroup\$

I would suggest a third approach that extends the conversion-to-string approach: conversion to List of Character

The benefits of this is that you can utilise Java 8 collection stream feature to perform filtering, aggregation and other functions on the elements.

for instance, your example can be expressed as such (edited following @h.j.k's comment):

 long num = 123456789; String s = String.valueOf(num); long count = s.chars() .mapToObj(i -> (char)i) .filter(ch -> ch.equals('1')) .count(); 
\$\endgroup\$
1
  • \$\begingroup\$ Wells, if you really want to go down this way, then there's String.chars() that will give you an IntStream directly. \$\endgroup\$ Commented Jul 23, 2015 at 9:46
1
\$\begingroup\$

This method below allows you to get a specific number from a int value based on a specific index

 public static int getSpecificNum(int number, int index) { int numOfDigits = 0; int pow = 1, test = 0, counter = 0; 

//gets the number of digits

 while (test != number) {// once the full int is retrieved counter++;//<-digit counter pow *= 10; test = number % pow;//go through the entire int } 

// number of digits are found, reset everything

 numOfDigits = counter; counter = 0; pow = 1; test = 0; 

// now count until the index

 while (counter != (numOfDigits - index)) {// this is numOfDigits was needed counter++; pow *= 10; test = number % pow;// same thing } 

// exp = finding the power of 10

 int exp = numOfDigits - (index + 1); exp = (int) Math.pow(10, exp); return test / exp;//divide and conquer } 
\$\endgroup\$
1
\$\begingroup\$

If you want to find the length of the number you don't need an array. Just using log base 10 will get you the number of places the number has so,

long num = 123456789; int length = (int) Math.log10(num); //should make the length 9 

Now to get a specific number it's

public int getNthDigit(int number, int base, int n) { return (int) ((number / Math.pow(base, n - 1)) % base); } 

If you enter getNthDigit(num,10,8); then it will return 2.

\$\endgroup\$
1
  • \$\begingroup\$ The approach to find the length of a number by taking log base 10 is incorrect. This does not work always. For example: If num is 10, your code will return 1 If num is 100, your code will return 2 If num is 1000, your code will return 3 Similarly, for other numbers (example 2), it will not return the length as 1. Math.log10(2) = 0.3010299956639812 and casting it to int will give 0 \$\endgroup\$ Commented Apr 2, 2020 at 17:17
0
\$\begingroup\$

Corner failure

while(num > 0) { ...} fails to get the right count when long num = 0; and the digit sought is 0. count result is 0. I'd expect 1.

Failure when num < 0 as int digit = num % 10; will result in a negative value.


Oops now see in the end: "Also let us assume it is for positive numbers." Making answer wiki as a reference.

Too bad as it is not difficult to make the code work for all int.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.