Among the myriad of Youtube channels I’m subscribed to, I saw Erik Hanchett’s video the other day. The one where he talked about his interview experience and the question he had worked on during his interview. Here is my take on that problem.
I love using Goodreads. There is a great community there; contributing to the site with their comments, reading lists and such. When I discover a new list such as the Riftwar Cycle, I want to copy the book names to a Google Sheet. When I visit a bookstore, I then pull up the file to see if I have already purchased it or not. So, my Google Sheet is ever changing but one thing stays constant. My rate of buying books is exponentially growing over the rate of reading what I already own.
Some lists are mixed, meaning they are part of a universe and there are many authors contributing to it. In that case, I would like to write down the author’s name next to the book’s title. You might wonder if it would be a better idea to group the items by author name first then the book title. I thought about doing it that way too first but then the books that I’m usually interested in are grouped by type so they are all clustered in one bookshelf rather than distributed all over the store by the author’s name.
So, our goal is to parse a list out of page content in the following format: Book Title, Author Name.
Most examples I see for reduce talk about summing up numbers. Can we use reduce to do more than punching numbers? Doesn’t it sound ironic that these accumulator or sum calculator examples look more like they are increasing some values rather than reducing? I know I’m pushing the envelope here. I think removing duplicates from an array would be a more suitable example of reducing the array we are working with. Because, after all, we would be reducing it to a minimal state.
How ever you define what reduce does, here is my take on using reduce to remove duplicates from an array. It only works for simple data types of course but you can enhance it the way you see it fit. For simplicity sake, I’ll attach my solution to Array.prototype. Again, this is optional.