Hash functions like MD5 can have collisions. This means two different files can have the same MD5 checksum.
I would calculate the MD5 when the file is uploaded and store the MD5 sum in a database. The database could be used to find files with the same checksum. The important part is before removing one of the files with a duplicate MD5 checksum I would compare the files that have matching MD5 checksums.
In the unix world you could use the "cmp" command line program. I would probably do the file comparison in Perl so the script did not have to fork child processes to run "cmp" and so the script will run on systems without "cmp".
.