Skip to content

Commit f201546

Browse files
committed
Added Acconts Merge program with Disjont set in Graphs section
1 parent c8157a0 commit f201546

File tree

2 files changed

+53
-24
lines changed

2 files changed

+53
-24
lines changed

src/main/java/com/thealgorithms/graph/DisjointSet.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -153,27 +153,4 @@ public List<List<String>> accountsMerge(List<List<String>> accountList) {
153153

154154
return mergedAccounts;
155155
}
156-
157-
}
158-
159-
/**
160-
*
161-
* this the program for testing the above algorithm
162-
*
163-
public class DisjointSetMain {
164-
public static void main(String[] args) {
165-
List<List<String>> list = new ArrayList<>();
166-
list.add(new ArrayList<>(List.of("abc", "[email protected]","[email protected]")));
167-
list.add(new ArrayList<>(List.of("abc","[email protected]","[email protected]")));
168-
list.add(new ArrayList<>(List.of("Mary","[email protected]")));
169-
list.add(new ArrayList<>(List.of("John","[email protected]")));
170-
list.add(new ArrayList<>(List.of("John", "[email protected]", "[email protected]")));
171-
DisjointSet ds = new DisjointSet(list.size());
172-
List<List<String>> ans = ds.accountsMerge(list);
173-
for(List<String> val : ans){
174-
System.out.println(val);
175-
}
176-
}
177-
}
178-
179-
*/
156+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.thealgorithms.graph;
2+
3+
4+
import org.junit.jupiter.api.Test;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import java.util.*;
7+
8+
public class DisjointSetTest {
9+
10+
@Test
11+
public void testAccountsMerge() {
12+
// Input data setup
13+
List<List<String>> list = new ArrayList<>();
14+
list.add(new ArrayList<>(List.of("abc", "[email protected]", "[email protected]")));
15+
list.add(new ArrayList<>(List.of("abc", "[email protected]", "[email protected]")));
16+
list.add(new ArrayList<>(List.of("Mary", "[email protected]")));
17+
list.add(new ArrayList<>(List.of("John", "[email protected]")));
18+
list.add(new ArrayList<>(List.of("John", "[email protected]", "[email protected]")));
19+
20+
// Create instance of your Solution/DisjointSet class
21+
DisjointSet disjointSet = new DisjointSet(list.size()); // or DisjointSet if that’s where accountsMerge() lives
22+
23+
// Execute the method
24+
List<List<String>> result = disjointSet.accountsMerge(list);
25+
26+
// Expected output (order of accounts may vary)
27+
List<List<String>> expected = new ArrayList<>();
28+
expected.add(Arrays.asList("abc", "[email protected]", "[email protected]", "[email protected]"));
29+
expected.add(Arrays.asList("Mary", "[email protected]"));
30+
expected.add(Arrays.asList("John", "[email protected]", "[email protected]"));
31+
32+
// Sort both results for deterministic comparison
33+
sortAccounts(result);
34+
sortAccounts(expected);
35+
36+
// Verify results
37+
assertEquals(expected, result);
38+
}
39+
40+
// Helper method to sort emails and accounts for deterministic comparison
41+
private static void sortAccounts(List<List<String>> accounts) {
42+
for (List<String> acc : accounts) {
43+
// Sort all emails (leave name first)
44+
List<String> emails = acc.subList(1, acc.size());
45+
Collections.sort(emails);
46+
}
47+
// Sort accounts lexicographically by name
48+
accounts.sort(Comparator.comparing(a -> a.get(0)));
49+
}
50+
}
51+
52+

0 commit comments

Comments
 (0)