Hackerank: Test challenges — Sales by Match.

Samuel Ezedi
2 min readJan 7, 2021

Hello friends, welcome to my channel, today we are going to be solving a problem I found on Hackerank. Actually, I found this problem in the interview preparation kit. So without further ado let’s get to it.

The Problem.

Alex works at a clothing store. There is a large pile of socks that must be paired 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=7 socks with colors ar = [1,2,1,2,1,3,2]. There is one pair of color 1 and one of color 2. There are three odd socks left, one of each color. The number of pairs is 2.

Function Description.

Complete the sockMerchant function in the editor below. It must return an integer representing the number of matching pairs of socks that are available.sockMerchant has the following parameter(s):n: the number of socks in the pilear: the colors of each sock

Input Format.

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 Format

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

Sample Input

9
10, 20, 20, 10, 10, 30, 50, 10, 20

Sample Output

3

Solution in Dart

Solution(In PHP)

Solution in Javascript(NodeJs)

Conclusion

To see an explanation of how I arrived at this solution watch my Youtube video and follow me on LinkedIn, Github, and Twitter.😎😎

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Samuel Ezedi
Samuel Ezedi

No responses yet

Write a response