I’ve got an interesting performance tuning/algorithmic problem that I’m running into in an optimization context.

I’ve got a set of ~16-50 lists of integers (usually in `Range(0, 5)`

but no restricted to that).

The data might look like this (although obviously not random)

```
maxQ = 5;
ndim = 16;
nstates = 100000;
braVecs = RandomInteger({0, maxQ}, {ndim, nstates});
ketVecs = braVecs + RandomInteger({0, 1}, {ndim, nstates});
```

now for every element `q â Subsets(ndim, 4)`

I need to determine where every pair of `braVecs`

and `ketVecs`

are the same, *except* for the indices in `q`

, i.e. I for every possible `q`

I need to calculate this

```
qComp = Complement(Range(ndim), q);
diffs = braVecs((qComp)) - ketVecs((qComp));
Pick(Range(nstates), Times @@ (1 - Unitize(diffs)), 1)
```

Just as an example, this is the kind of thing I expect to get out

```
q = RandomSample(Range(ndim), 4);
qComp = Complement(Range(ndim), q);
diffs = braVecs((qComp)) - ketVecs((qComp));
{q, Pick(Range(nstates), Times @@ (1 - Unitize(diffs)), 1)}
{{2, 9, 6, 4}, {825, 1993, 5577, 5666, 9690, 9856, 11502, 13515, 15680, 18570,
19207, 23131, 26986, 27269, 31889, 39396, 39942, 51688, 52520, 54905, 55360,
60180, 61682, 66258, 66458, 68742, 71871, 78489, 80906, 90275, 91520, 93184}}
```

This can obviously be done just by looping, but I *sure* there is an algorithmically more efficient way to do this, maybe using a Trie or maybe using some Boolean algebra to reuse prior calculations? This is important because my `ndim`

can get up to ~50 and there are a huge number of elements in `Subsets(50, 4)`

… I just don’t know what the best way to approach this kind of thing is.