Hacker Rank: Extracting digits from a given number and check for divisibility The problem in question is a coding challenge from Hackerrank:


  Problem Statement
  
  You are given an integer N. Find the digits in this number that
  exactly divide N (division that leaves 0 as remainder) and display
  their count. For N=24, there are 2 digits (2 &amp; 4). Both of these
  digits exactly divide 24. So our answer is 2.
  
  Note
  
  If the same number is repeated twice at different positions, it should
  be counted twice, e.g., For N=122, 2 divides 122 exactly and occurs at
  ones' and tens' position. So for this case, our answer is 3. Division
  by 0 is undefined. Input Format
  
  The first line contains T (the number of test cases), followed by T
  lines (each containing an integer N).
  
  Constraints
  1≤T≤15
   0 &lt; N &lt;10 ^ 10
  
  Output Format
  
  For each test case, display the count of digits in N that exactly 
  divide N in a separate line.


Here's my code: 

package algorithms.Warmup;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created by user1 on 4/2/15.
 */
public class FindDigitsTest {

    static Scanner sc = new Scanner(System.in);

    private static Long[] findDigits(long num) {

        ArrayList&lt;Long&gt; digits = new ArrayList&lt;&gt;();

        //extract all digits from input number and store in arraylist
        while (num &gt; 0) {

            long quotient = num / 10;
            long remainder = num - (quotient * 10);
            num = quotient;
            digits.add(remainder);

        }

        Long[] result = new Long[digits.size()];
        result = digits.toArray(result) ;
        return result;

    }

    private static int findCount(Long[] list, long num) {

        int count = 0;
        for(int i = 0; i &lt; list.length; i++) {

            //ignore division by zero- DON'T FORGET!!!
            if(list[i] == 0) {
                continue;
            }
            if (num % list[i] == 0){
                count++;
            }
        }

        return count;

    }
    public static void main(String[] args) {

        ArrayList&lt;Integer&gt; al = new ArrayList&lt;&gt;();

        //read in number of test cases
        System.out.println("Enter number of test cases: ");
        int T = sc.nextInt();

        //loop through number of test cases
        for(int i = 0; i &lt; T; i++) {

            System.out.println("Enter the number: ");
            long N = sc.nextLong();

            //extract digits of number and store in array
            Long[] nDigits = findDigits(N);

            //calculate num. of digits which exactly divide N
            int answer = findCount(nDigits, N);

            //store answer in final arraylist
            al.add(answer);

        }

        //loop through final arraylist to print answers
        for(int i = 0; i &lt; al.size(); i++) {
            System.out.println(al.get(i));
        }


    }
}


Even though the submission is correct, and the output is as desired, I feel there are many ways this can be improved, I just don't know how. I see that I have too many ArrayLists and Arrays here and there throughout the code, maybe that can be replaced to begin with.

I'm not completely proficient in Java yet, so my knowledge of Java APIs and Collections is still somewhat limited. That might be one reason for the inefficient code.
I see that I have too many ArrayLists and Arrays here and there throughout the code, maybe that can be replaced to begin with.


Yes, that would probably be a good idea. In findDigits you internally use an ArrayList, I'm guessing because it's a lot easier to use than an array (which is true, a List is the correct collection type here). But then you transform it to an array, which isn't really needed. 

Returning lists instead of arrays is perfectly acceptable in Java, and is in fact used in most cases (an exception might be made for fixed length arrays in performance critical code, eg coordinates).

So just change your signature from Long[] findDigits(long num) to List&lt;Long&gt; findDigits(long num), and then use a List in findCount as well.

Misc


declare variables in as small a scope as possible. sc isn't really needed anywhere except in main, so declare it there.
don't use short variables names. sc would be clearer as scanner and al could be results.
findDigits could be getDigits, after all you are not really searching for the digits or computing them (they are right there), just getting them.
findCount could be countDivisors, and then num could be divisor.
the question says that you are given an integer, but you use long instead.
your while loop in findDigits could be simplified to digits.add(num % 10);  num = num / 10;.