Sales by Match

  • + 0 comments

    Sort array then loop through to count pairs:

    matches = 0 sortedSocks = sorted(ar)

    if n == 1:
        return 0
    
    i = 0
    while i < n - 1:
        if sortedSocks[i] == sortedSocks[i+1]:
            matches += 1
            i += 2
        else:
            i += 1
    
    return matches