program dups implicit none integer, dimension(1:12) :: example1 integer, dimension(1:15) :: example2 integer, dimension(1:23) :: example3 ! input example1 = (/1, 2, 3, 2, 2, 4, 5, 5, 4, 6, 6, 5/) example2 = (/1, 1, 2, 2, 2, 4, 8, 5, 8, 6, 6, 5, 5, 5, 5/) example3 = (/1, 2, 3, 2, 2, 4, 5, 5, 4, 6, 6, 5, 20, 20, 0, 0, 0, 0, 6, 9, 22, -1, -1/) call finddupe(example1,size(example1)) call finddupe(example2,size(example2)) call finddupe(example3,size(example3)) contains subroutine bubble_sort(a) implicit none integer, intent(in out), dimension(:) :: a integer :: temp integer :: i, j logical :: swapped = .true. do j = size(a)-1, 1, -1 swapped = .false. do i = 1, j if (a(i) > a(i+1)) then temp = a(i) a(i) = a(i+1) a(i+1) = temp swapped = .true. end if end do if (.not. swapped) exit end do end subroutine bubble_sort subroutine finddupe(inp,n) implicit none integer,intent(in) :: n integer :: k,m integer, intent(in), dimension(1:n) :: inp ! input integer, dimension(1:n) :: array ! output integer, dimension(1:n) :: dupe ! output integer, dimension(1:n) :: cnt ! output k=1 cnt=1 array=inp ! sort input array call bubble_sort(array) do m=1,n-1 if (array(m)==array(m+1)) then cnt(k)=cnt(k)+1 dupe(k)=array(m) else k=k+1 endif enddo write(*,*) "input=", inp write(*,*) "sorted=", array do m=1,n if (cnt(m)>1) then write(*,*) "duplicated element is", dupe(m), " and count =", cnt(m) endif enddo end subroutine finddupe end program dups |