Hey hey, welcome to my first Let's Solve! We're going to be solving an algorithm code challenge called Picking Numbers, which is categorized as an ...
For further actions, you may consider blocking this person and/or reporting abuse
I like the approach! Maybe a bit of feedback:
The necessity of using comments to structure code is often an indication for a function being too long. Your code is already well structured, so you can directly extract the parts of your code as functions:
This has several benefits:
Here's a solution (minus the parsing) using exclusively functions from the utility library ramda. Each number is assigned to 2 potential clusters and all that's left is to find the most common cluster.
There is quite some functional lingo in there, but it doesn't require much javascript knowledge.
repl:
ramdajs.com/repl/?v=0.25.0#?const%...
Great job on solving the problem!
Just some pointers for future reference:
new Array(100)
is pretty wasteful, but I'll get back to this in a secmap.fill(0)
in your code isn't doing anything because you don't assign the return value back to map. Array.prototype.fill is not a modifier functionJavaScript can be pretty confusing at time but I hope you stick around and again nice work :D
Hey Sean, thanks for the response! I think for the way that I'm approaching the problem, that I actually need to initialize the array with 100 indices - otherwise, I couldn't map the keys to their array index positions. Also, I think
fill
does modify array values, as well as return the outcome of the fill. I tested that out on JSFiddle.(Your avatar is dope btw. :))
The solution and explanation are great.
should be using , instead of ;. And make use of a meaning name. I would also look into making use of built-in Map in ES6.
`
I love the way you approached the problem. Shows how well you understand the techniques you opted to use.
Thanks man! :) I appreciate that!
Ryan -
absolute difference
in problem statement should be changed to difference only as this solution of using array indexes does not work when we have negative numbers in the input array.