intersection.js 639 B

123456789101112131415161718192021222324252627
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. module.exports = function(arrays) {
  8. const result = new Set(arrays[0]);
  9. let resultSize = result.length;
  10. let i, value, valuesToCheck;
  11. for (i = 1; i < arrays.length; i++) {
  12. valuesToCheck = new Set(arrays[i]);
  13. for (value of result) {
  14. if (!valuesToCheck.has(value)) {
  15. result.delete(value);
  16. resultSize -= 1;
  17. }
  18. if (resultSize === 0) {
  19. return [];
  20. }
  21. }
  22. }
  23. return Array.from(result);
  24. };