Array integer[]: how to get all distinct values in a table and count them?
You can use UNNEST.

select unnest(ports) as port, count(*) from foo group by port;


Using more than one UNNEST in the same query (or the same select list, anyway) is confusing and is probably best avoided.