I am currently working on this problem on Leetcode. I noticed that depending on how I initialize the array that will hold the adjacency lists for each course, I get a different answer.
let graph = new Array(numCourses).fill(0).map(() => []) // works
let graph = new Array(numCourses).fill([]) // does not work
I logged the value of graph
after initialization to the console and for both cases, I get [ [], [] ]
for my first test case. I was expecting them both to work as from what I can see, they are both arrays of equal length containing empty arrays. However, the graph
that was set by just filling with empty arrays did not produce the expected outcome.
Is there difference bewteen arrays that are filled and then mapped to some value and those that are just filled with that value?