DEV Community

Play Button Pause Button
Ady Ngom
Ady Ngom

Posted on

Javascript Sock Merchant Challenge - Solution 2

You can find Part 1 here

The challenge

Hacker Rank Sock Merchant Page

John works at a clothing store. He has a large pile of socks that he must pair 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.

The code


Enter fullscreen mode Exit fullscreen mode


javascript
function stockAndCount( n, arr ) {
let pairs = 0;
const colors = arr.reduce((acc, val) => {
(!!acc[val]) ? acc[val] += 1 : acc[val] = 1;
return acc;
}, {});

Object.keys(colors).forEach( n => {
    let _pair = parseInt( colors[n] / 2);
    if ( _pair >= 1 ) pairs += _pair;
});

return pairs;
Enter fullscreen mode Exit fullscreen mode

}


Enter fullscreen mode Exit fullscreen mode

Video Transcript

In part 1 we solved the challenge using a sort first and compare approach, let’s clean up and look at an alternative solution.

Using the stockAndCount function, we will create an object that will stock each of our colors as keys.

So we will still create a pairs variable, then we will have a colors variable here but using the reduce method here we will be building up this object as we go.

In the reduce callback we setup an accumulator and the current value - doing this on the fly we check to see if our current value exists as a key in our accumulator object, if so we add one to it if not we create the key and initialize it with 1.

Let’s not forget to add the empty object as the second argument and return the accumulator after each iteration

Let’s make sure the function is properly closed

What we have done above is what I call the Dictionary approach.

Now that we have an object with each color as a key ket’s loop through it.

We iterate through each key and we create a local pair variable. We initialize the pair by dividing the colors n key value by 2

Now we can check if the pair value is greater or at least equal to 1. If true we can increment the total pairs value on line 17 with the number of pairs found

We can then simply return the total count after the loop. Running it in the terminal gives us again 3 pairs - If that went too fast let’s add to our console statement and run it again

We first used a sort and count approach in the last episode and today we looked at the Dictionary approach to stock each color as key and total up the pair count.

In the next and final episode we will discuss why the second approach is a better approach. In the meantime please let me know what you think about each and why one might be better than the other in the comments.

Cheers

Top comments (28)

Collapse
 
qm3ster profile image
Mihail Malo • Edited

I'd write the provided code as follows:

function stockAndCount( n, arr ) {
  const colors = {}
  for (const color of arr)
    if (colors[color]) colors[color]++
    else colors[color] = 1

  return Object.values(colors).reduce((sum, b) => sum + (b/2|0), 0)
}
Enter fullscreen mode Exit fullscreen mode

Comments:

  1. Why is pairs defined at the top, when it's only used for the final summation? This increases cognitive overhead, since we need to dig deeper to realize it's not being used in the reduce loop.
  2. Why the reduce? We are mutating a single object by reference, and using reduce just forces us to thread the reference through.
  3. Why the explicit boolean coercion? It relies on the same truthiness rules the conditional expression does.
  4. Why a conditional expression? We aren't using the expression return value, we are performing side effects upon the acc object.
  5. Why Object.keys if we only care about values, regardless of the color?
  6. Why parseInt? It's slower and less idiomatic than Math.floor (I used |0 to make it "integer division")
  7. Why the if condition? Even if we did get 0 sometimes, x+=0 is fine to do, and more functional.

My impression of this code is that it's an improved algorithm, but looks to be in the state of an unfinished refactor.

Collapse
 
theodesp profile image
Theofanis Despoudis • Edited

I don't think you even need the last reducer. Just keep a running counter.

function sockMerchant(n, ar) {
    const colors = {}
    let pairs = 0;
    for (const color of ar)
        if (colors[color]) {
            colors[color] = 0;
            pairs += 1;
        } else {
            colors[color] = 1;
        }

    return pairs;
}

Most of the times it reports submillisecond cost with 10000 elements

Call to sockMerchant took 0.22000004537403584 milliseconds.

@Ady can you check this?

Collapse
 
qm3ster profile image
Mihail Malo • Edited

Another hilarious one:

function sockMerchant(n, ar) {
    const colors = {}
    let pairs = 0
    for (const color of ar) pairs += !(colors[color] = !colors[color])

    return pairs
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
johnboy5358 profile image
John

... and just when I thought it could not get better...

Sweet!

Thread Thread
 
adyngom profile image
Ady Ngom

Nice indeed 😀

Thread Thread
 
johnboy5358 profile image
John

MM has come up with some JS wizardry here ... flipping those 1's and 0's ... I would'nt have come up with it myself, but hats off to him; it's a cracker!

I wonder if he plays code golf?

Thread Thread
 
qm3ster profile image
Mihail Malo

MM is afraid he doesn't.
I did golf a dweet once.

Thread Thread
 
johnboy5358 profile image
John

Oh! how cool is that!

I could watch that all day in fullscreen mode, but I wouldn't get anything else done.

Cheers for sharing.

Thread Thread
 
adyngom profile image
Ady Ngom

It's surprisingly calming and on my big screen monitor truly awesome indeed

Thread Thread
 
johnboy5358 profile image
John

Oh! now your just boasting Ady ... how big?

Would be an excellent screen saver; don't you think?

Thread Thread
 
adyngom profile image
Ady Ngom

boasting lol :) I've converted a 40" flat screen tv into a monitor. the image quality is exquisite and yes a great screen saver

Thread Thread
 
johnboy5358 profile image
John

Oh my!

That's what I would call ... total immersion!

Collapse
 
adyngom profile image
Ady Ngom

Hi Theofanis, thanks for adding to the discussion. I totally agree that the last reducer is not needed and when it comes to performance, the for loop is always going to outperform the functional methods for large datasets. What is more interesting is the possibility to make it in one pass and that's where I believe your solution shines the most.

I even took a stab at that approach in one of my previous solutions, see code below.

function sockMerchant(n, ar) {
    let _colors = {};
    let pairs = 0;
    for(let i = 0; i < n; i++) {
        _colors[ar[i]] = (!!_colors[ar[i]]) ? _colors[ar[i]] + 1 : 1;
        // do it in one pass
        if( (_colors[ar[i]] % 2) == 0 ) pairs +=1;
    }
    return pairs;
}

const n = 9;
const socks = [10, 20, 20, 10, 10, 30, 50, 10, 20];

console.group('Sorted and counted');
    console.log(`There is a total of ${sockMerchant(n, socks)} pairs`);
console.groupEnd();

The difference with yours ultimately is that your colors value can only be 1, 2 or 0 which I believe for the context of this exercise - just counting the pairs - is perfect.

The one above since it's keep adding to the total of stocked socks, needs to do a division by 2 anytime a new sock is added.
I will of course run all against the benchmark and since JS Perf is back up and running, I'll put them all there and make a public share of the results for documentation purpose.

Cheers

Collapse
 
johnboy5358 profile image
John

Nice solution!

Collapse
 
qm3ster profile image
Mihail Malo

@theodesp this was just me commenting on the code without changing the algorithm at all.
My own solution (which does use a counter) was posted separately:

My own solution would be:

function stockAndCount( n, arr ) {
  let pairs = 0
  const unpaired = new Set()

  for (const color of arr)
    if (unpaired.has(color)) {
      pairs++
      unpaired.delete(color)
    } else unpaired.add(color)

  return pairs
}

This doesn't iterate again over the colors encountered, and uses the minimal memory required for the task: The running total of pairs and the unpaired colors, encountering which again would be the completion of a pair.

Collapse
 
johnboy5358 profile image
John

An alternative solution:


const a = [10, 20, 20, 10, 10, 30, 50, 10, 20]
// [[10, 10, 10, 10], [20, 20, 20], [30], [50]]
// => 3 complete pairs (& 3 odd socks)

const group = (p,c) =>
  (c in p)
    ? {...p, [c]: p[c] + 1}
    : {...p, [c]: 1}

// Note: ~~ is Math.floor in js.
const sumPairs = (acc, x) => acc + ~~(x/2)

// Note: .map can be used for function composition provided you wrap the input ary in an array => [ary].
// for further info Visit https://egghead.io/lessons/javascript-linear-data-flow-with-container-style-types-box

const completePairs = (n, ary) =>
  [ary]
    .map(a => a.reduce(group, {}))
    .map(Object.values)
    .map(a => a.reduce(sumPairs,0))
    .pop()

console.log(completePairs(9, a))


Collapse
 
adyngom profile image
Ady Ngom

Hello John, I have definitely enjoyed reading your post and have learned a few nice tricks from it. My issue though with the solution will be readability first. It really takes a minute to grasp what is going on here. The second one is that a lot of transforms (map chain) are performed here where they probably could be taken care of by one computation reducing the complexity and improving the performance. Running against a simple benchmark this solution is over 300% slower than the proposed one.
I will host a webinar probably next week that will focus on why this second approach might be more suitable for this type of challenges. It will mean the world to me if you could attend and add to the discussion

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.

Collapse
 
johnboy5358 profile image
John • Edited

Hello Ady, thanks for the reply.

I recognize that my solution must look a bit strange on first sight. I was reading on MDN recently about the draft proposal for the pipeline operator aka |> to be added to JavaScript(as yet unsupported).

I also had passed sometime on the egghead.io site
re: egghead.io/courses/professor-frisb... and was intrigued how .map is used here to produce a topdown workflow composing(or piping) one function's result to the next function. What a shame that, as you have discovered, JS is not performant in this way, because, I for one, think this is an elegant and clean workflow that I hope, one day, will be worth using.

Of course speed of execution is always an important issue and so for the moment, atleast, we fall back to the roots of JavaScript and use it's for, for of, while, etc loops. Shame, because I really do like a more declarative style of coding and here I was merely experimenting.

Perhaps you will forgive a quick hack to make my solution a tad more readable(see code below) by placing a pipe method on the Array prototype. Sorry, I can't do anything about JS performance, but I think somewhat less than 10,000 socks to sort would be favourable; that sock merchant needs to keep his house in closer order I think!

Incidentally, please excuse me if I decline the webinar participation; it really is not my thing and you may take this reply as my input to that.

Have a great day.


if(!Array.prototype.pipe) Array.prototype.pipe = Array.prototype.map
const reduce = (fn, init) => xs => Array.prototype.reduce.call(xs, fn, init)

const a = [10, 20, 20, 10, 10, 30, 50, 10, 20]
// [[10, 10, 10, 10], [20, 20, 20], [30], [50]]
// => 3 complete pairs (& 3 odd socks)

const group = (p,c) =>
  (c in p)
    ? {...p, [c]: p[c] + 1}
    : {...p, [c]: 1}

// Note: ~~ is Math.floor in js.
const sumPairs = (acc, x) => acc + ~~(x/2)

// Note: .pipe is function composition. Pipes the result of a function to the next.
// Visit https://egghead.io/lessons/javascript-linear-data-flow-with-container-style-types-box
const completePairs = (n, ary) =>
  [ary]
    .pipe(reduce(group, {}))
    .pipe(Object.values)
    .pipe(reduce(sumPairs,0))
    .pop()

console.log(completePairs(9, a))
// => 3

As a final request, I would be interested in what benchmark timings (in milliseconds) you achieved for my soloution vs your solution for a test array of 10,000 integers.

Collapse
 
theodesp profile image
Theofanis Despoudis

Looks complicated. Take a look at my comment.

Collapse
 
johnboy5358 profile image
John

Thanks, I did and borrowing your concept I found a reducing callback that works, but I think it is going to show what Ady said about functional code in JS with reference execution speed. Array.prototype.reduce is slow compared to other JS imperative looping constructs. However, I am getting somewhere around 30ms for the code below using console.time on a 10k set.

When put in perspective; a blink of an eye is approx 300 - 400ms, roughly a third of a second, so it's still pretty quick.


const sockMerchant = (n, ar) =>
  ar.reduce((acc, colr) => {
    return (colr in acc && acc[colr])
      ? {...acc, [colr]:0, 'pairs':acc['pairs']+1}
      : {...acc, [colr]:1}
  }, {'pairs': 0} ).pairs

Thread Thread
 
adyngom profile image
Ady Ngom

Hey John awesome to have you back and yet coming up with other ways of enriching the discussion. I really, really like what you are proposing here.

It really boils down to having an imperative or a declarative style in solving those problems. Your solution here is kind a mix of both. For those who are used to reduce this might look declarative even though the ternary operation is leaning more towards imperative.

If those concepts are new or not clear, I can't recommend enough Tyle McGinnis article on the subject: Imperative vs Declarative Programming

Regarding performance, I will quote what I think is a real gem from the above article:

Many (if not all) declarative approaches have some sort of underlying imperative abstraction.

-- Tyler McGinnis

That should settle the performance discussion since declarative (map, reduce, filter, etc...) will always include and abstract one or more imperative layers such as a for loop. I can live with 30ms for a dataset of 10,000 items though if I have the understanding that the for loop might cut in half, it is an informed decision.

The last part that is left from my perspective would be readability. I think it is highly readable for the trained eye but might still be hard to digest for the enthused and upcoming at first glance. With time though, one might learn to grow and appreciate the details in it.

Cheers

Thread Thread
 
johnboy5358 profile image
John

Thanks Ady, Tyler's article is interesting and he also says in that same article...

"Imperative: C, C++, Java

Declarative: SQL, HTML

(Can Be) Mix: JavaScript, C#, Python"

JavaScript, as he points out, is a mixed bag and I think we should take every opportunity to use that wherever it works best. Yes, I have been having fun trying out different solutions.

But, I think, that's all from me on the sock merchant. I had a thought that I might go and learn a functional programming language like Elm or Elixir.

Thanks again for the Sock Merchant problem... it has been fun!

Thread Thread
 
adyngom profile image
Ady Ngom

You will be missed Sir John, thank you and happy coding :)

Collapse
 
qm3ster profile image
Mihail Malo • Edited

My own solution would be:

function stockAndCount( n, arr ) {
  let pairs = 0
  const unpaired = new Set()

  for (const color of arr)
    if (unpaired.has(color)) {
      pairs++
      unpaired.delete(color)
    } else unpaired.add(color)

  return pairs
}

This doesn't iterate again over the colors encountered, and uses the minimal memory required for the task: The running total of pairs and the unpaired colors, encountering which again would be the completion of a pair.

Collapse
 
adyngom profile image
Ady Ngom • Edited

Hello Mihail and thank you for both of your detailed inputs. I have a final episode and webinar coming up to allow us in taking a detail look at why one solution over the other. If you go the Hacker Rank site you will in fact find numbers of way that this one challenge is solved. So it will be pretentious from me to say that this one is the best one of all the possible ones. But when it comes to the first solution provided in episode 1 and this one, this is by far a better solution as far as approach and performance.

I have added the two functions you have provided and run all three against a simple benchmark to see how each performs against a set of 10,000 items. Check the screenshot below:

sock merchant stock and count benchmark

// First solution episode 1
$node sockMerchant.js 
4997
Sort And Count: 74.724ms

// This solution episode 2
$node sockMerchant.js 
4997
Stock And Count: 16.788ms

// Your first solution
$node sockMerchant.js 
4997
Stock And Count 2: 49.022ms

// Your second solution
$node sockMerchant.js 
4997
Stock And Count 3: 77.768ms

I have call and run each of those separately running the setup below:

console time to run benchmarks

I really like the points you raised regarding side effects and type coercion. I will make sure that those are addressed during the webinar.

It will be great if you could join and offer some insights.

Cheers :)

Collapse
 
qm3ster profile image
Mihail Malo

Yup, my "highly scientific" browser benchmark also makes it seem that Set is slower:

function socks_set(arr) {
  let pairs = 0
  const unpaired = new Set()

  for (const color of arr)
  if (unpaired.has(color)) {
      pairs++
      unpaired.delete(color)
    } else unpaired.add(color)

  return pairs
}
function socks_obj(ar) {
  const colors = {}
  let pairs = 0
  for (const color of ar) pairs += !(colors[color] = !colors[color])

  return pairs
}
function* good() {
  let i = 0
  while (i < 100000000) yield i++ >> 1
  return i >> 1
}
function* bad() {
  let i = 0
  while (i < 10000000) yield i++
  return i
}

console.log(socks_set(good()))
console.log(socks_obj(good()))
console.time("good Set")
socks_set(good())
console.timeEnd("good Set")
console.time("good Obj")
socks_obj(good())
console.timeEnd("good Obj")
console.time("good Set")
socks_set(good())
console.timeEnd("good Set")
console.time("good Obj")
socks_obj(good())
console.timeEnd("good Obj")

console.log(socks_set(bad()))
console.log(socks_obj(bad()))
console.time("bad Set")
socks_set(bad())
console.timeEnd("bad Set")
console.time("bad Obj")
socks_obj(bad())
console.timeEnd("bad Obj")
console.time("bad Set")
socks_set(bad())
console.timeEnd("bad Set")
console.time("bad Obj")
socks_obj(bad())
console.timeEnd("bad Obj")