85. Sock Drawer Pairs

After a laundry cycle, a tangled drawer of loose socks needs sorting. The array colours lists the colour code of every sock in the drawer, one entry per sock. Two socks can be paired only when they have the same colour code, and each sock can belong to at most one pair.

Return the maximum number of pairs that can be formed from the socks in the drawer. Socks left without a partner, such as the odd one out of a colour seen three times, are simply ignored.

Example 1

Input:
colours = [3,1,3,3,1,2]
Output:
2
Explanation:

Colour 3 appears three times (one pair) and colour 1 appears twice (one pair), giving 2 pairs.

Example 2

Input:
colours = [5,6,7]
Output:
0
Explanation:

Every colour appears once, so no pair can be formed.

Constraints

1 ≤ colours.length ≤ 105

0 ≤ colours[i] ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…