0

How can I count how many of a digit is in an array?

13th May 2019, 11:22 PM
Mimi
Mimi - avatar
5 Respostas
+ 3
int[] numbers = new int[10]; System.out.println(numbers.length); //output 10
14th May 2019, 12:58 AM
Dlite
Dlite - avatar
+ 3
Mimi Is the array type numeric (int) or string? if it was string array, you can use `String.contains` method to verify whether a string (the number) contains another string (digit/number you want to search). But that method only checks for existence, it doesn't count number of occurrence. If it was an int array then you can use modulo operator to check for a digit in each array element. Mind that you may need to calculate the divisor in case the number you seek for is more than one digit.
14th May 2019, 10:55 PM
Ipang
+ 2
array.Lenght?
13th May 2019, 11:49 PM
_yaroslavv [online_everyday]
_yaroslavv [online_everyday] - avatar
0
D'Lite _yaroslavv [online_everyday] No, I mean if I have an array like: int[] arr = {10,4,8,43,4}; And I want to know how many 4's are in the array (3). How do I do this?
14th May 2019, 3:26 PM
Mimi
Mimi - avatar
0
In other words: is there a method like: str.has(digit); Or something?
14th May 2019, 3:45 PM
Mimi
Mimi - avatar