Search code examples
fortran

How to count number of occurences in a Fortran array


For example if I have a 1d array

indices=(/1, 1, 1, 2 , 2 ,10, 11 /)

I want to know how many times 1 occurs (the answer should be 3).

Number 2 should be 2 times, number 10 should be 1, number 11 should also be 1.

I already tried to find out if there is intrinsic function but the count function works differently.


Solution

  • You can use the count intrinsic with a comparison operator, e.g.

    integer :: indices(7)
    indices = [1, 1, 1, 2, 2, 10, 11]
    write(*,*) count(indices==1)