Ctrl K

Matching Sock Pairs

Easy
After laundry day, a basket holds a pile of socks; socks[i] is the color of sock i. The number of socks is even. Can all the socks be split into pairs so that both socks in every pair share a color and every sock is in exactly one pair? Return True if so, and False otherwise.
Example 1
socks=[5, 2, 5, 2, 2, 2] → True

The pairs are (5, 5), (2, 2) and (2, 2): four socks of color 2 make two pairs.

Example 2
socks=[7, 7, 3, 1] → False

The socks of colors 3 and 1 are left without a match.

Example 3
socks=[9, 9] → True
Constraints
  • len(socks) is even
  • 2 ≤ len(socks) ≤ 1000
  • 1 ≤ socks[i] ≤ 500
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWords From Working Keys EasyPairs With the Same Letter Set EasyWidest Group With a Shared Bit MediumThe Candidate With Most Votes EasyThe Lost Ticket Easy
loading Python…
Input
socks=[5, 2, 5, 2, 2, 2]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 86 tests on the server, hidden ones included.