Skip to content
Advertisement

How do I get the difference between two FIFO array states?

I have two arrays that represent a fifo-like state, an old state and a new state. I need a function that finds the newly added items by comparing the new array with the old one. Below 3 examples of two arrays where 1 has items added to the front of it compared to the other:

JavaScript

Note that the amount of newly added items is removed from the back of the array. Here the desired functionality getItemsAdded(arr1, arr2) function:

JavaScript

It feels like such a simple problem, but I cant get my head around it.. I couldn’t solve it with solutions provided here How to get the difference between two arrays in JavaScript?, since its a different problem.

Advertisement

Answer

Code can tell more words, Then my silly explanation…

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement