Well, you can start of with:
SELECT name, COUNT(*) AS name_count FROM names GROUP BY name ORDER BY name_count DESC;
So, this would return name/counts in the order of your example data:
Hans - 5
James - 3
Peter - 2
Now, the basic idea is to loop through this array (from 1 less than the largest number to 1), keeping a count of the number of people who have a count above the current number. If there are no names associated with the current number, skip it. When designing this algorithm, you might want to consider repeated name counts.