Sales by Match (HackerRank Javascript Solution)

There is a large pile of socks that must be paired by color. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.

Example

n = 7
ar = [1, 2, 1, 2, 1, 3, 2]
There is…


This content originally appeared on DEV Community and was authored by DEV Community

There is a large pile of socks that must be paired by color. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.

Example

n = 7
ar = [1, 2, 1, 2, 1, 3, 2]
There is one pair of color and one of color . There are three odd socks left, one of each color. The number of pairs is.

Function Description

Complete the sockMerchant function in the editor below.

sockMerchant has the following parameter(s):

  • int n: the number of socks in the pile
  • int ar[n]: the colors of each sock

Returns

  • int: the number of pairs

Input Format

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

function sockMerchant(n, ar) {
    // Write your code here
    const uniqueValues = [... new Set(ar)]
    let count = 0
    uniqueValues.forEach(value => {
        const filterValues = ar.filter(element => element == value)
        const pairsNumber = Math.floor(filterValues.length/2)
        count += pairsNumber
    })
    return count
}


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-02-23T10:59:22+00:00) Sales by Match (HackerRank Javascript Solution). Retrieved from https://www.scien.cx/2022/02/23/sales-by-match-hackerrank-javascript-solution/

MLA
" » Sales by Match (HackerRank Javascript Solution)." DEV Community | Sciencx - Wednesday February 23, 2022, https://www.scien.cx/2022/02/23/sales-by-match-hackerrank-javascript-solution/
HARVARD
DEV Community | Sciencx Wednesday February 23, 2022 » Sales by Match (HackerRank Javascript Solution)., viewed ,<https://www.scien.cx/2022/02/23/sales-by-match-hackerrank-javascript-solution/>
VANCOUVER
DEV Community | Sciencx - » Sales by Match (HackerRank Javascript Solution). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/23/sales-by-match-hackerrank-javascript-solution/
CHICAGO
" » Sales by Match (HackerRank Javascript Solution)." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/02/23/sales-by-match-hackerrank-javascript-solution/
IEEE
" » Sales by Match (HackerRank Javascript Solution)." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/02/23/sales-by-match-hackerrank-javascript-solution/. [Accessed: ]
rf:citation
» Sales by Match (HackerRank Javascript Solution) | DEV Community | Sciencx | https://www.scien.cx/2022/02/23/sales-by-match-hackerrank-javascript-solution/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.