
About the author
Mallikarjuna Mallisetty
General programming
Mallikarjuna shares practical programming tutorials and foundational concepts designed to help developers learn by building and experimenting.
View LinkedIn profile ↗#include <stdio.h>
#include <string.h>
#define MAX_ACCOUNTS 4
#define MAX_EMAILS 5
#define MAX_EMAIL_LEN 50
#define MAX_NAME_LEN 20
// Simple structure to hold account data
typedef struct {
char name[MAX_NAME_LEN];
int emailCount;
char emails[MAX_EMAILS][MAX_EMAIL_LEN];
} Account;
// Helper function to check if email exists in an array
int emailExists(char emails[][MAX_EMAIL_LEN], int count, const char *email) {
for (int i = 0; i < count; i++) {
if (strcmp(emails[i], email) == 0) {
return 1;
}
}
return 0;
}
int main() {
Account accounts[MAX_ACCOUNTS] = {
{"John", 2, {"johnsmith@mail.com", "john00@mail.com"}},
{"John", 1, {"johnnybravo@mail.com"}},
{"John", 2, {"johnsmith@mail.com", "john_newyork@mail.com"}},
{"Mary", 1, {"mary@mail.com"}}
};
// Merging accounts that share emails (naive approach for demonstration)
// Result stored in merged accounts
Account merged[MAX_ACCOUNTS];
int mergedCount = 0;
for (int i = 0; i < MAX_ACCOUNTS; i++) {
int mergedIndex = -1;
for (int j = 0; j < mergedCount; j++) {
// Check if any email overlaps
for (int e1 = 0; e1 < accounts[i].emailCount; e1++) {
if (emailExists(merged[j].emails, merged[j].emailCount, accounts[i].emails[e1])) {
mergedIndex = j;
break;
}
}
if (mergedIndex != -1) break;
}
if (mergedIndex == -1) {
// Add new merged account
merged[mergedCount++] = accounts[i];
} else {
// Merge emails without duplicates
for (int e1 = 0; e1 < accounts[i].emailCount; e1++) {
if (!emailExists(merged[mergedIndex].emails, merged[mergedIndex].emailCount, accounts[i].emails[e1])) {
strcpy(merged[mergedIndex].emails[merged[mergedIndex].emailCount++], accounts[i].emails[e1]);
}
}
}
}
// Print merged accounts
for (int i = 0; i < mergedCount; i++) {
printf("%s: ", merged[i].name);
for (int e = 0; e < merged[i].emailCount; e++) {
printf("%s%s", merged[i].emails[e], (e == merged[i].emailCount - 1) ? "\n" : ", ");
}
}
return 0;
}