#Way to check if two slices contain the same items (ignoring order) in a test
1 messages · Page 1 of 1 (latest)
I am confused, do you care about order or not
I don't care about order
oh, you are probably stating the equality functions you do see check order...
misread, mybad
this is definitely not the most efficient way to do it but it works
fn eqlUnordered(a: []const u8, b: []const u8) bool {
if (a.len != b.len) return false;
const a_major = for (a) |ai| {
for (b) |bi| {if (bi == ai) break;}
else break false;
} else true;
const b_major = for (b) |bi| {
for (a) |ai| {if (bi == ai) break;}
else break false;
} else true;
return a_major and b_major;
}
if there are never duplicate items you can remove b_major.
You don't care about order, but it still might be worth considering sorting? Hard to say without knowing more (const or var slice, size of data, is one of the slices contents always known etc)
Sorting is probably the easiest way to get something relatively fast. You can probably modify some sorting algorithms to get early exit, but more difficult
another option would be counting the amount of every kind of value in a map