Skip to content

Instantly share code, notes, and snippets.

@sheldonhull
Last active December 27, 2020 01:42
Show Gist options
  • Select an option

  • Save sheldonhull/f7671b1d78705c329fe25b3c4af7c3ec to your computer and use it in GitHub Desktop.

Select an option

Save sheldonhull/f7671b1d78705c329fe25b3c4af7c3ec to your computer and use it in GitHub Desktop.
[Algos - Sock Merchant] Return matching pairs of socks #algorithms #go #puzzles #golang
// Hackerrank submission for https://www.hackerrank.com/challenges/sock-merchant/submissions/code/193045923
// Complete the sockMerchant function below.
func sockMerchant(n int, ar []int) int {
// possiblePairs := int(math.Floor(float64(n) / 2))
sort.Slice(ar, func(i, j int) bool { return ar[i] < ar[j] })
// sort.Sort(byValue(ar))
var currentPairMatched int = 0
// size := len(ar)
for i := 0; i < len(ar)-1; i++ {
if ar[i] == ar[i+1] {
fmt.Sprintf("Matched pair: %v with %v", ar[i], ar[i+1])
currentPairMatched++
i++
if i >= len(ar){
break
}
}
}
//return int(currentPairMatched)
// fmt.Sprintf("possible pairs: %f",possiblePairs)
// fmt.Printf("%s",currentPairMatched)
return int(currentPairMatched)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment