Home > Mobile >  Count number of individual digits in an array of ints?
Count number of individual digits in an array of ints?

Time:02-02

So, this is not on how to count digits in a number. It's how to count how many of each one. Say:

arr{101, 103, 105, 107, 109, 110}

How many 0s, how many 1s etc. I wanna put it into a TreeMap

TreeMap<Integer, Integer> tm = new TreeMap<Integer, Integer>();
tm = [0 -> 5, 1 -> 8, 2 -> 0, 3 -> 1, ..., 9 -> 1]

So how should I go about doing that?


The array will be of whatever size so I guess I can loop through it and for each new int check each digit and if d = 0 then tm.put(d, [increase]) etc. Not sure how to increase a value in a KV-map though.

CodePudding user response:

Using a TreeMap is quite overkill and you should be fine using an array where the index corresponds to the digit (eg. index 0 is the number of 0's and index 5 is the number of 5's).

To extract the digits from the number you can use % 10 to get the remainder from dividing by 10 which will be the lowest digit, increment that counter for that digit and then divide the number by 10 until you've gone through all the digits.

The code below should be good enough and doesn't modify any of the values in the source array.

    int array[] = {101, 103, 105, 107, 109, 110};
    int digitCount[] = new int[10];
    
    for (int i = 0; i < array.length; i  ) {
        int num = array[i];
        while (num != 0) {
            int digit = num % 10;
            digitCount[digit]  ;
            num /= 10;
        }
    }

CodePudding user response:

It wasn't exactly clear to me whether you wanted to know how to extract the digits from the numbers. So I included that too. You could do this with a stream, but Map features in Java 8 make it easier, imo. You could also store these in an array but you asked for a map based solution so here it is.

int[] values = { 101, -23003, 22, 1, 3, -1001, 0, 0, 111 };
Map<Integer, Integer> results = new TreeMap<>();

  • iterate thru the values.
  • get the absolute value of each (allows for negative numbers).
  • within a do/while loop,
    • compute adds 1 to the exiting value or initializes value to 1 first time thru.
    • %get the remainder when divided by some number. In this case 10.
    • divide by 10 to expose the next digit

It's important to check for 0 after the first value is added to allow for a 0 value in the original list.


for (int val : values) {
    int n = Math.abs(val);
    do {
        results.compute(n % 10,
                (k, v) -> v == null ? 1 : v   1);
        n /= 10;
    } while (n > 0);
}

results.entrySet().forEach(System.out::println);

prints

0=7
1=8
2=3
3=3

CodePudding user response:

Since you say "I guess I can loop through it and for each new int check each digit" I will assume you know how to extract the digits from the integers and I will just answer this part: "Not sure how to increase a value in a KV-map though."

To sum the digits in the map, you can do something like this:

Map<Integer, Integer> counts = new TreeMap<>();
// your source of the digits will be different
for (int digit : new int[] { 1, 1, 1, 2, 2, 3 }) {
    counts.merge(digit, 1, Integer::sum);
}

The map will then contain {1=3, 2=2, 3=1} (three 1s, two 2s, one 3).

Of course, you will need to feed it the digits you extract from the integers, rather than a pre-set list of digits, but the method for combining the values in the KV-map will be the same.

What the merge method of the map does is it sets the key (d, the digit) to 1, unless the key already exists in the map, in which case it merges the new value 1 with the existing value (the count so far) by calling Integer::sum which sums them up.

  •  Tags:  
  • Related