Search code examples
javascriptdeque

Cannot find module "collections/deque"


Trying to use Deque data structure to answer a programming problem to find all sub arrays with a product less than the target.

As mentioned, I want to use a Deque Data structure. I looked at the usage and think I'm doing it right but by using

const Deque = require("collections/deque");

However, I'm getting the error:

Cannot find module "collections/deque"

If anybody can see what I'm missing please let me know.

This is the code.

const Deque = require("collections/deque");

function find_subarrays(arr, target) {
  let result = [],
    product = 1,
    left = 0;
  for (right = 0; right < arr.length; right++) {
    product *= arr[right];
    while (product >= target && left < arr.length) {
      product /= arr[left];
      left += 1;
    }
    const tempList = new Deque();
    for (let i = right; i > left - 1; i--) {
      tempList.unshift(arr[i]);
      result.push(tempList.toArray());
    }
  }
  return result;
}

Solution

  • Problem is collections.js does not come with vanilla JS you have to install it using npm

    npm install --save collections
    

    Or just use built in array method to simulate deque operations.