[Solved] Count Numbers of 0 and 1 in an array


Assuming you only continues values(0,1,2,3….)

int[] A = {0, 0, 0, 0, 1, 1, 1, 2, 2, 2}; // any number and values will work!

int max = IntStream.of(A).max().getAsInt(); // gets max value in array
int[] counter = new int[max + 1]; 
for (int i = 0; i < A.length; ++i)
    counter[A[i]]++;

4

solved Count Numbers of 0 and 1 in an array