fbpx

Sock Merchant Problem: How to Solve using Python?

solution

Sock Merchant Problem: How to Solve using Python?

The below problem is a very famous Online problem, that can be solved in number of ways. We have solved below the Sock Merchant problem in the most efficient way using Python.

Problem Statement:

Raj works at a clothing store. He has a large pile of socks that he must pair by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. For example, there are n = 9 socks with colors ar = [10,20,30,10,20,10,40,50,40]. There is one pair of color  10, one pair of color 20 and one pair of color 40. There are three odd socks left, one of each color. The number of pairs is 3.

works at a clothing store. He has a large pile of socks that he must pair by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. For example, there are n = 9 socks with colors ar = [10,20,30,10,20,10,40,50,40]. There is one pair of color  10, one pair of color 20 and one pair of color 40. There are three odd socks left, one of each color. The number of pairs is 3.

Input:

The first line contains an integer n, the number of socks represented in ar.
The second line contains n space-separated integers describing the colors ar[i] of the socks in the pile.

Output:

Return the total number of matching pairs of socks that Raj can sell.

Solution:

Fig. 1: Sock Merchant Challenge Solution

Readers can feel free to write an email to the author in case of any concerns or doubt. Feel free to like our fb page to stay updated about more such solutions. Click here to visit the fb page.

Leave a Reply