Skip to content

[FEATURE REQUEST] Add Disjoint Set (Union-Find) Implementation and Account Merge Problem in Graph Section #6831

@Sanso2619

Description

@Sanso2619

What would you like to Propose?

While exploring the Graph folder and its problems, I noticed that the implementation of an important graph concept — Disjoint Set (Union-Find) — is currently missing. This data structure is crucial for handling problems that involve the dynamic connectivity of nodes, such as merging components or detecting cycles efficiently.

I’d like to contribute by:

Implementing a Disjoint Set class following proper OOP principles (with union by rank and path compression).

Adding a practical problem based on this concept — Merging Accounts.

for example
Input:
[["abc","abc@mail.com","abx@mail.com"],
["abc","abc@mail.com","aby@mail.com"],
["Mary","mary@mail.com"],
["John","johnnybravo@mail.com"]]

Output:
[["abc","abc@mail.com","abx@mail.com", "aby@gmail.com"],
["Mary","mary@mail.com"],
["John","johnnybravo@mail.com"]]

Two accounts belong to the same person if they share at least one common email.
Even if two accounts have the same name, they might belong to different people, so merging should only be based on shared emails. Each person can have multiple accounts, and all merged accounts should have the same name.

Issue details

You are given a list of accounts where each element accounts[i] is a list of strings.
The first element, accounts[i][0], represents the name, and the remaining elements represent the emails belonging to that account.

Your task is to merge accounts that belong to the same person.
Two accounts belong to the same person if they share at least one common email.
Note that even if two accounts have the same name, they may belong to different people, as multiple users can share the same name.
A person can have any number of accounts, but all their accounts will have the same name.

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions