---
title: "Combine JavaScript sets in C#"
url: https://recursiveai.org/oneecma/cookbook/combine-javascript-sets/
description: "Run JavaScript Set union, intersection, and difference from C# to compare lists of IDs. Includes a .NET example with checked results."
version: "1.1.1"
---

# Combine JavaScript sets in C#

**OneEcma** is a free, open-source JavaScript interpreter for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneEcma) · [GitHub](https://github.com/recursive-ai-oss/oneecma-csharp) · [Docs](https://recursiveai.org/oneecma/docs/) · [Download example (.zip)](https://recursiveai.org/oneecma/cookbook/combine-javascript-sets/combine-javascript-sets.zip)

Find the union, intersection, and difference of two ID lists.

```shell
dotnet add package RecursiveAI.OneEcma
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneEcma;
var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
const old = new Set([101, 102]);
const next = new Set([102, 103]);
[old.union(next), old.intersection(next),
 old.difference(next)]
  .map(ids => [...ids].join(', ')).join('\n');
""").AsString);
```

Unzip [the example](https://recursiveai.org/oneecma/cookbook/combine-javascript-sets/combine-javascript-sets.zip), then `dotnet run`.

Prints the union, intersection, and difference, in order:

```text
101, 102, 103
102
101
```

## The full program

```csharp
using RecursiveAI.OneEcma;

var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
const before = new Set([101, 102, 102, 104]);
const after = new Set([102, 103, 104]);
const results = {
  all: [...before.union(after)],
  kept: [...before.intersection(after)],
  removed: [...before.difference(after)],
  added: [...after.difference(before)]
};
if (JSON.stringify(results) !==
    '{"all":[101,102,104,103],"kept":[102,104],"removed":[101],"added":[103]}') {
  throw new Error('Unexpected ID sets');
}
Object.entries(results)
  .map(([label, ids]) => `${label}: ${ids.join(', ')}`)
  .join('\n');
""").AsString);
```

`dotnet run` prints:

```text
all: 101, 102, 104, 103
kept: 102, 104
removed: 101
added: 103
```

The download checks the results against the expected IDs before printing. The repeated `102` in the first input appears only once. Pass a `Set` or another set-like object as the argument: an array by itself lacks the required methods, so passing it directly throws a JavaScript `TypeError`.

## Variations

**Find all changes at once.** Use `symmetricDifference` when you want IDs that appear in exactly one set without separating additions from removals:

```csharp
using RecursiveAI.OneEcma;

var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
const before = new Set([101, 102, 104]);
const after = new Set([102, 103, 104]);
[...before.symmetricDifference(after)].join(', ');
""").AsString);
```

```text
101, 103
```

**Match IDs against a map.** A `Map` is set-like: `intersection` uses its keys, not its values:

```csharp
using RecursiveAI.OneEcma;

var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
const selected = new Set([101, 102]);
const labels = new Map([[102, 'Ready']]);
[...selected.intersection(labels)].join(', ');
""").AsString);
```

```text
102
```

## Related recipes

- [Exchange JavaScript typed arrays with .NET](https://recursiveai.org/oneecma/cookbook/exchange-javascript-typed-arrays/)
- [Call a JavaScript function from C#](https://recursiveai.org/oneecma/cookbook/call-javascript-function/)

## The short answer

Run JavaScript’s `Set` methods inside OneEcma. For sets named `before` and `after`, `before.union(after)` gives every distinct ID, `before.intersection(after)` gives IDs present in both, and `before.difference(after)` gives IDs no longer present. The C# example prints the results as text. The original sets remain available unchanged.

Use OneEcma when the set logic is already written in JavaScript and shared with another app; if the logic lives entirely in C#, use .NET’s `HashSet<T>` directly.

## What’s the difference between JavaScript Set union and intersection?

Use [`union`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/union) for an inclusive list: an ID can be in either input or both. Use [`intersection`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/intersection) for a common list: an ID must be in both. The downloaded program labels these `all` and `kept`. It converts the result sets to arrays with spread syntax (`[...set]`) so their members can be checked and printed.

Union starts with the receiver’s IDs in order, then adds new IDs from the argument. It doesn’t sort: `103` appears after `104` in the full program. Intersection can iterate the smaller side, so do not assume its output always follows the receiver’s insertion order. For a report that requires sorted IDs, sort the result separately from finding the matches.

## How do you find the difference between two JavaScript sets?

[`difference`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/difference) is directional. In the downloaded example, `before.difference(after)` contains only `101`, which was removed. `after.difference(before)` contains only `103`, which was added. [`symmetricDifference`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/symmetricDifference) gives both, leaving out any ID that occurs in both sets. If the report needs to say which IDs to add and which to remove, keep the two directional results instead.

## Can you use JavaScript Set intersection with arrays?

Wrap each array with `new Set(...)` first, as the snippet at the top does. This [compares values](https://recursiveai.org/oneecma/docs/collections-proxy/) without coercing their types: number `102` and string `'102'` are different keys. Objects match by identity, so separately created records with the same fields don’t match. Compare the ID values rather than whole records if the two lists come from separate reads of the same data.

JavaScript [set composition](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#set_composition) requires the receiver to be a set, but the argument may be a set-like object with `size`, `has`, and `keys`. That’s why the map in the variation works and a bare array doesn’t. A set also discards multiplicity: use it for membership, not for counting how many times an ID appears in a list.
