From 9102d8c79d038d41285e55454bfbef3475fef16c Mon Sep 17 00:00:00 2001 From: Abdullah Date: Sun, 19 Oct 2025 06:39:30 +0300 Subject: [PATCH 1/5] implemented add account --- .../thealgorithms/graph/MergeAccounts.java | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 src/main/java/com/thealgorithms/graph/MergeAccounts.java diff --git a/src/main/java/com/thealgorithms/graph/MergeAccounts.java b/src/main/java/com/thealgorithms/graph/MergeAccounts.java new file mode 100644 index 000000000000..e9888bdd05f0 --- /dev/null +++ b/src/main/java/com/thealgorithms/graph/MergeAccounts.java @@ -0,0 +1,125 @@ +package com.thealgorithms.graph; +/** + * 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"]] + +0, 1 Share Name + +0, 1 Share email + + + +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. + + + * + * + */ +import java.util.List; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +public class MergeAccounts { + + private static List> originalAccounts = new ArrayList<>(); + private static List Unneeded = new ArrayList(); +// { +// {"abc", "abc@mail.com", "abx@mail.com"}, +// {"abc", "abc@mail.com", "aby@mail.com"}, +// {"Mary", "mary@mail.com"}, +// {"John", "johnnybravo@mail.com"} +// }; + public static void main(String[] args) { + originalAccounts.add(new ArrayList<>(Arrays.asList("abc", "abc@mail.com", "abx@mail.com"))); + originalAccounts.add(new ArrayList<>(Arrays.asList("abc", "abc@mail.com", "aby@mail.com"))); + originalAccounts.add(new ArrayList<>(Arrays.asList("Mary", "mary@mail.com", "many@mail.com"))); + originalAccounts.add(new ArrayList<>(Arrays.asList("Mohammed", "meedo@mail.com"))); + originalAccounts.add(new ArrayList<>(Arrays.asList("Mary", "johnnybravo@mail.com","mary@mail.com"))); + originalAccounts.add(new ArrayList<>(Arrays.asList("Mohammed", "meedo@mail.com", "Mooda@mail.com"))); + + + System.out.println(originalAccounts); + // originalAccounts = mergeAccounts(); + mergeAccounts(); + System.out.println(originalAccounts); + } + public static void mergeAccounts(){ + for (int account = 0; account < originalAccounts.size(); account++) { + for (int otherAccount = account+1; otherAccount < originalAccounts.size(); otherAccount++) { + if (account != otherAccount){ + if(sameNameCheck(account,otherAccount)&&sameEmailCheck(account,otherAccount)){ + overlapItems(account, otherAccount); + } + + + } + } + + } + removeUnneeded(); + // return originalAccounts; +// + } + + public static void removeUnneeded() { + Collections.sort(Unneeded); + System.out.println(Unneeded); + for (int a = Unneeded.size()-1; a > -1 ; a--) { + int i = Unneeded.get(a); + System.out.println(i); + originalAccounts.remove(i); + } + } + public static boolean sameNameCheck(int acc1, int acc2) { + return(originalAccounts.get(acc1).get(0).equals(originalAccounts.get(acc2).get(0))); + } + + public static boolean sameEmailCheck(int acc1, int acc2) { + // originalAccounts[acc1] originalAccounts[acc2] + // array of 3 + int acc1Size = originalAccounts.get(acc1).size(); + int acc2Size = originalAccounts.get(acc2).size(); + for (int email = 1; email < acc1Size; email++) { + for (int otherEmail = 1; otherEmail < acc2Size; otherEmail++) { + // System.out.println("Compared "+ originalAccounts[acc1][email]+ " and "+ originalAccounts[acc2][otherEmail]); + if (originalAccounts.get(acc1).get(email).equals(originalAccounts.get(acc2).get(otherEmail))) + return true; + } + } + return false; + + } + public static void overlapItems(int acc1, int acc2) { + int acc1Size = originalAccounts.get(acc1).size(); + int acc2Size = originalAccounts.get(acc2).size(); + if (acc1Size < acc2Size){ + int temp = acc1; + acc1 = acc2; + acc2= temp; + temp = acc1Size; + acc1Size = acc2Size; + acc2Size = temp; + } + for (int email = 1; email < acc1Size; email++) { + for (int otherEmail = 1; otherEmail < acc2Size; otherEmail++) { + if (!originalAccounts.get(acc1).contains(originalAccounts.get(acc2).get(otherEmail))) + originalAccounts.get(acc1).add(originalAccounts.get(acc2).get(otherEmail)); + + } + } + Unneeded.add(acc2); + + + } + +} \ No newline at end of file From ac0359ef89038c66e8c0a21c62b30ead7ec17924 Mon Sep 17 00:00:00 2001 From: Abdullah Date: Mon, 20 Oct 2025 21:48:35 +0300 Subject: [PATCH 2/5] Working --- src/main/java/com/thealgorithms/graph/MergeAccounts.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/graph/MergeAccounts.java b/src/main/java/com/thealgorithms/graph/MergeAccounts.java index e9888bdd05f0..746538723ce6 100644 --- a/src/main/java/com/thealgorithms/graph/MergeAccounts.java +++ b/src/main/java/com/thealgorithms/graph/MergeAccounts.java @@ -31,6 +31,7 @@ import java.util.Collections; public class MergeAccounts { + private static List> originalAccounts = new ArrayList<>(); private static List Unneeded = new ArrayList(); // { @@ -73,10 +74,10 @@ public static void mergeAccounts(){ public static void removeUnneeded() { Collections.sort(Unneeded); - System.out.println(Unneeded); + // System.out.println(Unneeded); for (int a = Unneeded.size()-1; a > -1 ; a--) { int i = Unneeded.get(a); - System.out.println(i); + // System.out.println(i); originalAccounts.remove(i); } } From a8da81c8d50d7fe15520093764b069c889a5400d Mon Sep 17 00:00:00 2001 From: Abdullah Date: Mon, 20 Oct 2025 22:08:00 +0300 Subject: [PATCH 3/5] Added Modularity --- .../thealgorithms/graph/MergeAccounts.java | 65 +++++-------------- 1 file changed, 15 insertions(+), 50 deletions(-) diff --git a/src/main/java/com/thealgorithms/graph/MergeAccounts.java b/src/main/java/com/thealgorithms/graph/MergeAccounts.java index 746538723ce6..62a1db6d4413 100644 --- a/src/main/java/com/thealgorithms/graph/MergeAccounts.java +++ b/src/main/java/com/thealgorithms/graph/MergeAccounts.java @@ -1,60 +1,25 @@ package com.thealgorithms.graph; -/** - * 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"]] - -0, 1 Share Name - -0, 1 Share email - - - -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. - - - * - * - */ import java.util.List; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; public class MergeAccounts { +/** + * Merges accounts that share at least one common email address. + * + *

This method takes a list of accounts, where each account is represented as a list of strings: + * the first element is the account holder's name, followed by one or more email addresses. + * It merges accounts that share both the same name and at least one email address into a single list, + * removing duplicates and redundant entries.

+ * + * @param inputList a list of accounts, each represented as [name, email1, email2, ...] + */ - - private static List> originalAccounts = new ArrayList<>(); private static List Unneeded = new ArrayList(); -// { -// {"abc", "abc@mail.com", "abx@mail.com"}, -// {"abc", "abc@mail.com", "aby@mail.com"}, -// {"Mary", "mary@mail.com"}, -// {"John", "johnnybravo@mail.com"} -// }; - public static void main(String[] args) { - originalAccounts.add(new ArrayList<>(Arrays.asList("abc", "abc@mail.com", "abx@mail.com"))); - originalAccounts.add(new ArrayList<>(Arrays.asList("abc", "abc@mail.com", "aby@mail.com"))); - originalAccounts.add(new ArrayList<>(Arrays.asList("Mary", "mary@mail.com", "many@mail.com"))); - originalAccounts.add(new ArrayList<>(Arrays.asList("Mohammed", "meedo@mail.com"))); - originalAccounts.add(new ArrayList<>(Arrays.asList("Mary", "johnnybravo@mail.com","mary@mail.com"))); - originalAccounts.add(new ArrayList<>(Arrays.asList("Mohammed", "meedo@mail.com", "Mooda@mail.com"))); - - - System.out.println(originalAccounts); - // originalAccounts = mergeAccounts(); - mergeAccounts(); - System.out.println(originalAccounts); - } - public static void mergeAccounts(){ + + private static List> originalAccounts; + public static List> mergeAccounts(List> inputAccounts){ + originalAccounts = inputAccounts; for (int account = 0; account < originalAccounts.size(); account++) { for (int otherAccount = account+1; otherAccount < originalAccounts.size(); otherAccount++) { if (account != otherAccount){ @@ -68,7 +33,7 @@ public static void mergeAccounts(){ } removeUnneeded(); - // return originalAccounts; + return originalAccounts; // } From 6dd4f901dfe2284e694c9c188f65b0ea3ad6bd5d Mon Sep 17 00:00:00 2001 From: Abdullah Date: Mon, 20 Oct 2025 22:10:28 +0300 Subject: [PATCH 4/5] add MergeAccounts utility for merging user accounts by shared emails --- src/main/java/com/thealgorithms/graph/MergeAccounts.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/graph/MergeAccounts.java b/src/main/java/com/thealgorithms/graph/MergeAccounts.java index 62a1db6d4413..b299fb976022 100644 --- a/src/main/java/com/thealgorithms/graph/MergeAccounts.java +++ b/src/main/java/com/thealgorithms/graph/MergeAccounts.java @@ -16,7 +16,7 @@ public class MergeAccounts { */ private static List Unneeded = new ArrayList(); - + private static List> originalAccounts; public static List> mergeAccounts(List> inputAccounts){ originalAccounts = inputAccounts; @@ -29,8 +29,8 @@ public static List> mergeAccounts(List> inputAccounts) } - } - + } + } removeUnneeded(); return originalAccounts; From 6b38e8ec56f7a54fd93dc2645e9e93a2d030a338 Mon Sep 17 00:00:00 2001 From: Abdullah Date: Mon, 20 Oct 2025 22:12:21 +0300 Subject: [PATCH 5/5] add MergeAccounts utility for merging user accounts by shared emails --- src/main/java/com/thealgorithms/graph/MergeAccounts.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/graph/MergeAccounts.java b/src/main/java/com/thealgorithms/graph/MergeAccounts.java index b299fb976022..cfd51676dc3f 100644 --- a/src/main/java/com/thealgorithms/graph/MergeAccounts.java +++ b/src/main/java/com/thealgorithms/graph/MergeAccounts.java @@ -16,7 +16,6 @@ public class MergeAccounts { */ private static List Unneeded = new ArrayList(); - private static List> originalAccounts; public static List> mergeAccounts(List> inputAccounts){ originalAccounts = inputAccounts; @@ -29,8 +28,8 @@ public static List> mergeAccounts(List> inputAccounts) } - } - + } + } removeUnneeded(); return originalAccounts;