How to COUNT duplicate rows?

The question leaves room for interpretation. This test case shows 2 nested steps:

CREATE TABLE tbl (ad_id int, distance int);
INSERT INTO tbl VALUES
  (510, 0), (956, 3), (823, 3), (880, 2)
, (523, 3), (467, 0), (843, 1), (816, 9)
, (533, 4), (721, 7), (288, 3), (900, 3)
, (526, 9), (750, 7), (302, 8), (463, 6)
, (742, 8), (804, 2), (62,  7), (880, 2)
, (523, 3), (467, 0), (843, 1), (816, 9)
, (533, 4), (721, 7), (288, 3), (900, 3)
, (526, 9), (750, 7), (302, 8), (816, 9)
, (533, 4), (721, 7), (288, 3), (900, 3)
, (533, 4), (721, 7), (288, 3), (396, 5)
;

How many duplicates per value?

SELECT ad_id, count(*) AS ct FROM tbl GROUP BY 1;

Result:

ad_id  | ct
-------+----
62     | 1
288    | 4
302    | 2
396    | 1
...

Read: ad_id 62 exists 1x, ad_id 288 exists 4x, ...

How to count how many times rows have duplicates?

SELECT ct, count(*) AS ct_ct
FROM  (SELECT ad_id, count(*) AS ct FROM tbl GROUP BY 1) sub
GROUP  BY 1
ORDER  BY 1;

Result:

 ct | ct_ct
----+-------
1   | 8
2   | 7
3   | 2
4   | 3

Read: 8 occurrences of "ad_id is unique", 7 occurrences of "2 rows with same ad_id", ...

db<>fiddle here


Just add count(*) to your select:

SELECT temp.ad_id, temp.distance as hits, count(*)
....