Skip to content

feat: registry-item - array - add intersectArrays #85

@tresorama

Description

@tresorama

Raw Code

/**
 * Return a new array that contains the intersection of two arrays: items that are in both arrays
 */
export function intersectArrays<TArrayItem>(
  a: TArrayItem[],
  b: TArrayItem[],
) {
  // early exits if one of the array has length 0
  if (a.length === 0 || b.length === 0) return [];

  const [shorter, longer] = a.length < b.length ? [a, b] : [b, a];
  const longerSet = new Set(longer);
  return shorter.filter(item => longerSet.has(item));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions