3 ms·
The takeaway is that you can't have the single best-defined array difference---it is badly adapted from the well-defined set difference. There are tons of possi
by lifthrasiir 8y ago
The takeaway is that you can't have the single best-defined array difference---it is badly adapted from the well-defined set difference. There are tons of possible values that `[1a, 1b, 1c, 1d, 2, 3] - [1e, 1f, 2]` can yield (where `1a` through `1f` all compare identically but have different object identities):
[1b, 1c, 1d, 3] (remove the first occurrence only, no matter the RHS has many occurrences)
[1a, 1b, 1c, 3] (remove the last occurrence only)
any of [1a, 1b, 1c, 3], [1a, 1b, 1d, 3] etc. (remove any single unspecified occurrence)
[1c, 1d, 3] (remove the first K occurrences where K is the multiplicity)
[1a, 1b, 3] (remove the last K occurrences)
any of [1a, 1b, 3], [1b, 1c, 3], [1c, 1d, 3] etc. (remove any K unspecified occurrences)
[1a, 1b, 1c, 1d, 3] (remove a whole group of equal occurrences only when multiplicites match)
[1a, 3] (flatten both the LHS and the RHS and calculate the set difference)
[1d, 3] (same as above, but the set flattening prefers later occurrences)
any of [1a, 3], [1b, 3], [1c, 3] or [1d, 3] (same as above, but the set flattening is unspecified)
any of [1a, 3], ..., [1d, 3], [1e, 3] or [1f, 3] (same as above, but without any guarantee of the identical element)
runtime error, as there are multiple same elements in the LHS and/or the RHS
[0, 0, -1, NaN, NaN, NaN] (okay, I'll stop being clever and calculate actual numeric differences)
See? The "array difference" is not enough. Something like the "array difference with multiple occurrences removed in the order of appearance" would work, but this refutes the OP's claim of the "array difference" being "readable" and "intuitive".
- ricardobeat 8y agoI don’t see what’s complex about it: make a copy of the first array excluding any elements that exist in the second. Only one outcome possible.
- lifthrasiir 8y agoThe multiplicity is important if you see an array as a bag, i.e. a multiset. Some may reasonably expect the pairwise difference, which is indeed a default mode for vectors and matrixes (even C++ has one, `std::valarray`). There are also tons of implementation concerns I haven't ever dabbled.