0

i am trying to solve some basic java question:

i have an array like int[] x = { 12, 24, 33 };. I need to break it into digits like {1, 2, 2, 4, 3 ,3} and then count the repeating numbers this way: 1:1, 2:2, 3:2, 4:1.

Until now i got this code but i can't save the digits into array. Can some one help me ?

public class targil_2_3 {
    public static void main(String[] args) {

        int[] x = { 12, 24, 33 };
        int[] ara = new int[x.length * 2];

        for (int i = 0; i < x.length; i++) {
            for (int j = 0; j < 2; j++) {  
                ara[j] = x[i] % 10;
                x[i] = x[i] / 10;
                System.out.println(ara[j]);
            }
        }            
    }       
}
1
  • 1
    Use ArrayList class instead of int[]. It'll be easier to add objects in it. Commented Oct 6, 2016 at 6:13

4 Answers 4

3

You dont need to store individual digits, you need to store just count for digits. Lets assume, that you're working with 10 based numbers, then code can looks like

public static void main(String[] args) {
    int[] x = { 12, 24, 33, 0, 10, 555 };

    int[] count = new int[10];
    for (int i = 0; i < x.length; i++) {
        int num  = x[i];
        if (num == 0) {
            count[0]++;
            continue;
        }

        while (num > 0) {
            count[num % 10]++;
            num = num / 10;
        }
    }

    System.out.println(Arrays.toString(count));
}

Output is

[2, 2, 2, 2, 1, 3, 0, 0, 0, 0]
Sign up to request clarification or add additional context in comments.

Comments

2
import java.util.Arrays;
import java.util.Map;
import static java.util.stream.Collectors.*;

public class Use {
    public static void main(String[] args) {
        int[] x = { 12, 24, 33 };
        Map<Integer, Long> result = Arrays.stream(x).boxed()
                                        .map(String::valueOf)
                                        .collect(joining())
                                        .chars().boxed()
                                        .collect(groupingBy(Character::getNumericValue, counting()));
        System.out.println(result); //prints {1=1, 2=2, 3=2, 4=1}
    }
}

Explanation

  1. First line convert an int[] to a Stream<Integer> (for each element)
  2. Convert Stream<Integer> to Stream<String>
  3. Reduce the Stream<String> to String
  4. Create a Stream<Integer> (for each digit)
  5. Count the occurences of each digit in a Map

Comments

1

we have only 10 decimal digits from 0 to 9 , [0..9]

so we make an array with length 10 , like count :

int count[] = new int[10];
for(int i = 0 ; i < x.length ; i++){
     if( x[i] == 0 ){
         count[0]++;
         continue;
     }
     while(x[i]!=0){
         int index = x[i] % 10;
         count[index]++;
         x[i] /= 10;
     }
}

then we will have the number of digits in count array , so we can print it :

for(int i = 0 ; i < 10 ; i++)
    System.out.println(i+" : "+count[i]);

if your data is so big it is better to use Map there are many ways to do this

1 Comment

Tnx, very good and easy done. i would add to the last part this line of code for(int i = 0 ; i < 10 ; i++) if(count[i]!=0) System.out.println(i+" : "+count[i]); In order to exclude not relevant numbers.
0

Digits are 0-9. Build a counter array of size 10, and count each digit extracted in the proper index of the counter array ("counter[digit]++").

Edit: Of- course, in the end you can build the desired result array based on the counter array.

For example: result[0] = "0:" + counter[0];"

Good Luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.