// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

/**
 * Returns all distinct elements that appear in any of the given arrays.
 *
 * @template T The type of the array elements.
 *
 * @param arrays The arrays to get the union of.
 *
 * @returns A new array containing all distinct elements from the given arrays.
 *
 * @example Basic usage
 * ```ts
 * import { union } from "@std/collections/union";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const soupIngredients = ["Pepper", "Carrots", "Leek"];
 * const saladIngredients = ["Carrots", "Radicchio", "Pepper"];
 *
 * const shoppingList = union(soupIngredients, saladIngredients);
 *
 * assertEquals(shoppingList, ["Pepper", "Carrots", "Leek", "Radicchio"]);
 * ```
 */
export function union<T>(...arrays: Iterable<T>[]): T[] {
  const set = new Set<T>();

  for (const array of arrays) {
    for (const element of array) {
      set.add(element);
    }
  }

  return Array.from(set);
}

// denoCacheMetadata={"headers":{"nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","cf-ray":"a223e6564f467db6-LAX","content-type":"text/typescript","cache-control":"public, max-age=31536000, immutable","alt-svc":"h3=\":443\"; ma=86400","date":"Tue, 28 Jul 2026 12:31:15 GMT","vary":"Origin","set-cookie":"__cf_bm=3ylcLvFke09UnEzGpE.uGnfIdTrT5bHGevAL6K0_0dg-1785241874.9667947-1.0.1.1-ye8kigsVOnq_5PBCcpPUT3OL4My0xmqZoCtKMNlm0EQYKKy8F5PkUlg3ATDDHrFTabyljRyFZScYv44GPqiigKXM4jE46Uv7f7_Xe.xLbkdNg_qCawo8jKkDaCQuk.Gb; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=Zlg%2BjY9McL7ydJfp2AlfZjNbalFO%2F09%2BdIIkdTkRny5o3xgLjGoqRPqiP8p%2BtlEstIIGsp%2FIdl0CmQoWCn7eZv%2BNVhgfRC6WXyLoPIbjYBVN%2FlkZvAs5RJs%3D\"}]}","server":"cloudflare","x-jsr-backend":"modules","x-robots-tag":"noindex","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","access-control-expose-headers":"*","cross-origin-resource-policy":"cross-origin","content-length":"1024","access-control-allow-origin":"*","etag":"\"c52d13f7354350632105c77ef7a2bc3d\"","x-content-type-options":"nosniff"},"url":"https://jsr.io/@std/collections/0.224.2/union.ts","time":1785241875}