Here is my code:
What would be the best way to do this?
If you are fetching row from the database why can not rather GROUP BY there and get the records. That's the preferred way of doing it.
Assuming that the column name is first_name
first_name
group by substr(first_name,1,1)