subroutine remove_list1_states_from_list2(list_1, list_2, list_1_size, list_2_size)
use util_mod, only: binary_search_ilut
integer, intent(in) :: list_1_size
integer, intent(inout) :: list_2_size
integer(n_int), intent(in) :: list_1(0:NIfTot, list_1_size)
integer(n_int), intent(inout) :: list_2(0:NIfTot, list_2_size)
integer :: i, counter, pos, min_ind
min_ind = 1
do i = 1, list_2_size
! Binary search list_1 to see if list_2(:,i) is in it.
pos = binary_search_ilut(list_1(:, min_ind:list_1_size), list_2(:, i), NifD + 1)
! If it is in list 1, remove the state by setting it to 0.
! If it isn't in list 1 (pos < 0) then we can still search a smaller list next time.
if (pos > 0) then
list_2(:, i) = 0
min_ind = min_ind + pos
else
min_ind = min_ind - pos - 1
end if
if (min_ind > list_1_size) exit
end do
! Now compress the new list by overwriting the removed states:
counter = 0
do i = 1, list_2_size
! If the state wasn't set to 0:
if (.not. all(list_2(:, i) == 0)) then
counter = counter + 1
list_2(:, counter) = list_2(:, i)
end if
end do
list_2_size = counter
end subroutine remove_list1_states_from_list2