Find unique strings in an array Returns array of unique strings and count
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | input_array(:) | |||
| character(len=:), | intent(out), | allocatable | :: | unique_array(:) | ||
| integer, | intent(out) | :: | nunique |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer, | private | :: | i | ||||
| logical, | private | :: | is_unique | ||||
| integer, | private | :: | j | ||||
| integer, | private | :: | n | ||||
| character(len=len), | private, | allocatable | :: | temp_unique(:) |
pure subroutine find_unique_strings(input_array, unique_array, nunique) !! Find unique strings in an array !! Returns array of unique strings and count character(len=*), intent(in) :: input_array(:) character(len=:), allocatable, intent(out) :: unique_array(:) integer, intent(out) :: nunique integer :: i, j, n logical :: is_unique character(len=len(input_array)), allocatable :: temp_unique(:) n = size(input_array) allocate (temp_unique(n)) ! Max possible size nunique = 0 do i = 1, n is_unique = .true. ! Check if we've already seen this string do j = 1, nunique if (strings_equal(input_array(i), temp_unique(j))) then is_unique = .false. exit end if end do if (is_unique) then nunique = nunique + 1 temp_unique(nunique) = input_array(i) end if end do ! Allocate output array with exact size and copy allocate (character(len=len(input_array)) :: unique_array(nunique)) unique_array = temp_unique(1:nunique) end subroutine find_unique_strings