#Way to check if two slices contain the same items (ignoring order) in a test

1 messages · Page 1 of 1 (latest)

dim trench
#

All slice-equality I see check order as well.

tawdry heart
#

I am confused, do you care about order or not

dim trench
#

I don't care about order

tawdry heart
#

oh, you are probably stating the equality functions you do see check order...

#

misread, mybad

tawdry heart
#

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.

lethal tree
#

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)

dull sierra
#

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

supple ice
#

another option would be counting the amount of every kind of value in a map