Unique Email Addresses

Easy
235.

Unique Email Addresses

Unique Email Addresses is an easy coding problem on arrays, strings and hash table, asked in Amazon, Google, Microsoft and Intuit interviews.

Every valid email is a local name plus '@' plus a domain name.

When delivering, the server ignores every period in the local name, and ignores the local name from the first '+' onward. Both rules apply only to the local name — the domain is used exactly as written.

Given an array of emails, return how many distinct addresses actually receive mail.

Example 1

Input
emails = ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output
2
Explanation
The first two both deliver to "testemail@leetcode.com".

Example 2

Input
emails = ["a@leetcode.com","b@leetcode.com","c@leetcode.com"]
Output
3

Example 3

Input
emails = ["a.b+c@x.com"]
Output
1

Constraints

  • 1 <= emails.length <= 20
  • Each email contains exactly one '@'.
  • Local and domain names use lowercase letters, '.' and '+'.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string[]} emails
 * @return {number}
 */
var numUniqueEmails = function(emails) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Unique Email Addresses — Solution in Python, Java & C++

Unique Email Addresses solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Unique Email Addresses is an easy coding problem on arrays, strings and hash table, asked in Amazon, Google, Microsoft and Intuit interviews.

Problem statement

Every valid email is a local name plus '@' plus a domain name.

When delivering, the server ignores every period in the local name, and ignores the local name from the first '+' onward. Both rules apply only to the local name — the domain is used exactly as written.

Given an array of emails, return how many distinct addresses actually receive mail.

Example 1

Input: emails = ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output: 2
Explanation: The first two both deliver to "testemail@leetcode.com".

Example 2

Input: emails = ["a@leetcode.com","b@leetcode.com","c@leetcode.com"]
Output: 3

Example 3

Input: emails = ["a.b+c@x.com"]
Output: 1

Constraints

  • 1 <= emails.length <= 20
  • Each email contains exactly one '@'.
  • Local and domain names use lowercase letters, '.' and '+'.

Hints for Unique Email Addresses

  1. Hint 1

    Split each address at the '@' — everything after it is untouched.

  2. Hint 2

    In the local part, cut at the first '+' and then delete every period.

  3. Hint 3

    Insert the normalised address into a hash set and return its size.

How to solve Unique Email Addresses

Normalise each address — cut the local part at the first +, delete its periods, keep the domain as is — and count distinct results with a set.

Approach

  1. Split at @ into local and domain.
  2. Truncate local at the first +, then remove every ..
  3. Insert local + "@" + domain into a set; return its size.

Why it works

Two addresses deliver to the same mailbox exactly when their normalised forms are equal, so distinct normalised forms count distinct recipients.

Complexity

  • Time — O(total characters)
  • Space — O(total characters)

Pitfalls

  • Apply both rules only to the local part; a . or + in the domain is significant.
  • Cut at + before removing periods, or a period after the + is removed pointlessly (harmless, but wasted).

Reference solution

Python

def numUniqueEmails(emails) -> int:
    seen = set()
    for email in emails:
        local, domain = email.split("@")
        local = local.split("+")[0].replace(".", "")
        seen.add(local + "@" + domain)
    return len(seen)

Java

public static int numUniqueEmails(String[] emails) {
    Set<String> seen = new HashSet<>();
    for (String email : emails) {
        int at = email.indexOf('@');
        String local = email.substring(0, at);
        String domain = email.substring(at);
        int plus = local.indexOf('+');
        if (plus >= 0) local = local.substring(0, plus);
        local = local.replace(".", "");
        seen.add(local + domain);
    }
    return seen.size();
}

C++

int numUniqueEmails(vector<string>& emails) {
    set<string> seen;
    for (string& email : emails) {
        size_t at = email.find('@');
        string local = email.substr(0, at);
        string domain = email.substr(at);
        size_t plus = local.find('+');
        if (plus != string::npos) local = local.substr(0, plus);
        string cleaned = "";
        for (char c : local) {
            if (c != '.') cleaned += c;
        }
        seen.insert(cleaned + domain);
    }
    return (int) seen.size();
}

JavaScript

var numUniqueEmails = function(emails) {
    const seen = new Set();
    for (let i = 0; i < emails.length; i++) {
        const at = emails[i].indexOf("@");
        let local = emails[i].slice(0, at);
        const domain = emails[i].slice(at);
        const plus = local.indexOf("+");
        if (plus >= 0) local = local.slice(0, plus);
        local = local.split(".").join("");
        seen.add(local + domain);
    }
    return seen.size;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Strings