Skip to content

Commit a8da81c

Browse files
Added Modularity
1 parent ac0359e commit a8da81c

File tree

1 file changed

+15
-50
lines changed

1 file changed

+15
-50
lines changed

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

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,25 @@
11
package com.thealgorithms.graph;
2-
/**
3-
* for example
4-
Input:
5-
[["abc","abc@mail.com","abx@mail.com"],
6-
["abc","abc@mail.com","aby@mail.com"],
7-
["Mary","mary@mail.com"],
8-
["John","johnnybravo@mail.com"]]
9-
10-
0, 1 Share Name
11-
12-
0, 1 Share email
13-
14-
15-
16-
Output:
17-
[["abc","abc@mail.com","abx@mail.com", "aby@gmail.com"],
18-
["Mary","mary@mail.com"],
19-
["John","johnnybravo@mail.com"]]
20-
21-
Two accounts belong to the same person if they share at least one common email.
22-
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.
23-
24-
25-
*
26-
*
27-
*/
282
import java.util.List;
293
import java.util.ArrayList;
304
import java.util.Arrays;
315
import java.util.Collections;
326
public class MergeAccounts {
7+
/**
8+
* Merges accounts that share at least one common email address.
9+
*
10+
* <p>This method takes a list of accounts, where each account is represented as a list of strings:
11+
* the first element is the account holder's name, followed by one or more email addresses.
12+
* It merges accounts that share both the same name and at least one email address into a single list,
13+
* removing duplicates and redundant entries.</p>
14+
*
15+
* @param inputList a list of accounts, each represented as [name, email1, email2, ...]
16+
*/
3317

34-
35-
private static List<List<String>> originalAccounts = new ArrayList<>();
3618
private static List<Integer> Unneeded = new ArrayList<Integer>();
37-
// {
38-
// {"abc", "abc@mail.com", "abx@mail.com"},
39-
// {"abc", "abc@mail.com", "aby@mail.com"},
40-
// {"Mary", "mary@mail.com"},
41-
// {"John", "johnnybravo@mail.com"}
42-
// };
43-
public static void main(String[] args) {
44-
originalAccounts.add(new ArrayList<>(Arrays.asList("abc", "abc@mail.com", "abx@mail.com")));
45-
originalAccounts.add(new ArrayList<>(Arrays.asList("abc", "abc@mail.com", "aby@mail.com")));
46-
originalAccounts.add(new ArrayList<>(Arrays.asList("Mary", "mary@mail.com", "many@mail.com")));
47-
originalAccounts.add(new ArrayList<>(Arrays.asList("Mohammed", "meedo@mail.com")));
48-
originalAccounts.add(new ArrayList<>(Arrays.asList("Mary", "johnnybravo@mail.com","mary@mail.com")));
49-
originalAccounts.add(new ArrayList<>(Arrays.asList("Mohammed", "meedo@mail.com", "Mooda@mail.com")));
50-
51-
52-
System.out.println(originalAccounts);
53-
// originalAccounts = mergeAccounts();
54-
mergeAccounts();
55-
System.out.println(originalAccounts);
56-
}
57-
public static void mergeAccounts(){
19+
20+
private static List<List<String>> originalAccounts;
21+
public static List<List<String>> mergeAccounts(List<List<String>> inputAccounts){
22+
originalAccounts = inputAccounts;
5823
for (int account = 0; account < originalAccounts.size(); account++) {
5924
for (int otherAccount = account+1; otherAccount < originalAccounts.size(); otherAccount++) {
6025
if (account != otherAccount){
@@ -68,7 +33,7 @@ public static void mergeAccounts(){
6833

6934
}
7035
removeUnneeded();
71-
// return originalAccounts;
36+
return originalAccounts;
7237
//
7338
}
7439

0 commit comments

Comments
 (0)