Hello Bhavesh, thank you for your input. This is the first solution and not necessarily the most performant one as I have stated in the video. If you look at the gist that was created for this project here Sock merchant solutions you will that the second solution that is coming in part 2 is using the dictionary approach tou have suggested
Object.keys(colors).forEach( n => {
let _pair = parseInt( colors[n] / 2);
if ( _pair >= 1 ) pairs += _pair;
});
return pairs;
}
Since this is unassuming of who is watching and what the level is, I start with the simple easy to grasp and evolve to the 'better' solution, better being relative here.
Stay tuned for the next episode and the reasoning why solution 2 will be a better approach.
Hello Bhavesh, thank you for your input. This is the first solution and not necessarily the most performant one as I have stated in the video. If you look at the gist that was created for this project here Sock merchant solutions you will that the second solution that is coming in part 2 is using the dictionary approach tou have suggested
javascript
function stockAndCount( n, arr ) {
let pairs = 0;
const colors = arr.reduce((acc, val) => {
(!!acc[val]) ? acc[val] += 1 : acc[val] = 1;
return acc;
}, {});
}
Since this is unassuming of who is watching and what the level is, I start with the simple easy to grasp and evolve to the 'better' solution, better being relative here.
Stay tuned for the next episode and the reasoning why solution 2 will be a better approach.
Cheers
Good work, Sir.