DEV Community

Discussion on: Javascript Sock Merchant Challenge - Solution 2

Collapse
 
johnboy5358 profile image
John

A somewhat faster solution.


const count = (ofValue, ary) => {
  let i = ary.length
  let c = 0
  while(i--){ if(ary[i] === ofValue) {c++} }
  return c
}

const completePairs = (n, Ary) =>
  Array
    .from(new Set(Ary))
    .reduce((acc, v) => acc + ~~(count(v, Ary) / 2), 0)

My hope is that this is more readable and fast enough to satisfy most users.

Thread Thread
 
adyngom profile image
Ady Ngom

I will take a closer look and since you don’t like to be put on the spot πŸ˜€ I think I want to have our discussion transcend the comments section with an article that captures the exchange.
In the meantime I’ll run a benchmark on it but as far as readability this seems easier to digest IMHO.
I have started to write in a more functional way and isolating the arity for flexible composition. I think in the end it comes down to choosing the right tool and style for the right job.