Skip to content

Conversation

nchlsb
Copy link

@nchlsb nchlsb commented Jul 19, 2021

I saw that Bag did not have an equality function yet. I'm submitting an implementation that is imperative and optimized because I assume that is desired, but below is more declarative solution as well. Both pass the three unit tests included in this pull request.

equals(bag: Bag<T>): boolean {
        let allKeysSet = this.toSet();
        allKeysSet.union(bag.toSet());
        const allKeysArray = allKeysSet.toArray();
        return allKeysArray.every(element => this.count(element) === bag.count(element));
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant