Skip to main content

Unique Email Addresses - Solution & Explanation

EasyArrayHash TableString20 min readAsked at: Amazon, Intuit, Google +2
Practice this problem

Problem Statement

Every valid email consists of a local name and a domain name, separated by the '@' sign. Besides lowercase letters, the email may contain one or more '.' or '+'.

  • For example, in "alice@leetcode.com", "alice" is the local name, and "leetcode.com" is the domain name.

If you add periods '.' between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. Note that this rule does not apply to domain names.

  • For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address.

If you add a plus '+' in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered. Note that this rule does not apply to domain names.

  • For example, "m.y+name@email.com" will be forwarded to "my@email.com".

It is possible to use both of these rules at the same time.

Given an array of strings emails where we send one email to each emails[i], return the number of different addresses that actually receive mails.

 

Example 1:

Input: emails = ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output: 2
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails.

Example 2:

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

 

Constraints:

  • 1 <= emails.length <= 100
  • 1 <= emails[i].length <= 100
  • emails[i] consist of lowercase English letters, '+', '.' and '@'.
  • Each emails[i] contains exactly one '@' character.
  • All local and domain names are non-empty.
  • Local names do not start with a '+' character.
  • Domain names end with the ".com" suffix.
  • Domain names must contain at least one character before ".com" suffix.

Approach Overview

Problem Overview: You receive a list of email addresses. Certain normalization rules apply to the local name: dots (.) are ignored and everything after a plus (+) is discarded before the @. The task is to apply these rules and count how many unique addresses actually receive mail.

Approach 1: Using Set to Handle Unique Emails (O(n * m) time, O(n * m) space)

Iterate through each email and normalize it before counting. Split the address into local and domain around the @. Inside the local part, remove everything after the first +, then delete all dots. Reconstruct the normalized address and insert it into a set. Because a set stores only unique elements, duplicate normalized emails are automatically ignored. The algorithm scans each character of every email once, giving O(n * m) time where n is the number of emails and m is the average length.

This approach relies heavily on fast membership checks from a hash table. Sets provide average O(1) insertion and lookup, making them ideal for uniqueness problems. The logic is straightforward and easy to implement in any language using basic string operations.

Approach 2: Email Transformation with String Array Manipulation (O(n * m) time, O(n * m) space)

This method explicitly transforms the email step by step using string arrays. First split the email using @ into two parts. Convert the local name into a character array or iterate through it with a pointer. Stop processing when a + is encountered and skip dots while building the cleaned local name. Finally combine the cleaned local name with the domain and insert the result into a set to track uniqueness.

The key difference is the manual transformation process. Instead of chained string operations, you iterate through characters and build the normalized string incrementally. This gives tighter control over processing and can slightly reduce intermediate allocations in some languages. The complexity remains O(n * m) because each character is processed at most once.

Both solutions treat the problem as normalization followed by deduplication. The normalization stage applies deterministic rules, and the set guarantees uniqueness. The problem primarily tests comfort with array iteration, string parsing, and hash-based collections.

Recommended for interviews: The set-based normalization approach is what interviewers expect. It demonstrates clean thinking: transform each email once, then rely on a hash set for uniqueness. Brute reasoning about duplicates shows understanding, but recognizing that normalization + hashing solves the problem efficiently shows stronger problem‑solving skill.

Approach 1: Using Set to Handle Unique Emails

In this approach, we'll use a set to store unique email addresses. We'll iterate over each email, split it into local and domain parts, then manipulate the local part by removing any characters after the first '+' and all periods '.' before rejoining with the domain. Finally, we'll add the standardized email to the set. The size of the set gives the count of unique emails.

In Python, we use a set to hold unique email addresses. We split each email by '@' to get local and domain parts. The local part is then split at the '+' to ignore everything beyond it and replaced '.' with ''. The processed local part is then combined with the domain part before adding to the set. The number of unique emails is the size of the set.

Code

Python

C++

Java

JavaScript

C

C#

Complexity

Time Complexity: O(N * M) where N is the number of emails and M is the maximum length of an email address.
Space Complexity: O(N) due to the storage of unique emails in a set.

Try this approach in the editor →

Approach 2: Email Transformation with String Array Manipulation

In this approach, instead of transforming emails directly using a set, we'll utilize arrays to manipulate the email parts. We'll maintain a boolean array for discovered unique emails and an index array to track up to where the email is unique. This efficient tracking helps us determine unique addresses without full storage, but more focused on transformations directly performed within the iteration loop.

This is similar to Approach 1, but a slight variation in explanation to describe transformations as stepwise and using string-based mutation directly. The Python simplicity helps accomplish similar efficiency with identical methods.

Code

Python

C++

Java

JavaScript

C

C#

Complexity

Time Complexity: O(N * M), since per email processed once fully.
Space Complexity: O(N), enabling efficient tracking of unique addresses.

Try this approach in the editor →

Approach 3: Hash Table

We can use a hash table s to store all unique email addresses. Then, we traverse the array emails. For each email address, we split it into the local part and the domain part. We process the local part by removing all dots and ignoring characters after a plus sign. Finally, we concatenate the processed local part with the domain part and add it to the hash table s.

In the end, we return the size of the hash table s.

The time complexity is O(L), and the space complexity is O(L), where L is the total length of all email addresses.

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Using Set to Handle Unique Emails

Time Complexity: O(N * M) where N is the number of emails and M is the maximum length of an email address.
Space Complexity: O(N) due to the storage of unique emails in a set.

Email Transformation with String Array Manipulation

Time Complexity: O(N * M), since per email processed once fully.
Space Complexity: O(N), enabling efficient tracking of unique addresses.

Hash Table—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Using Set to Handle Unique EmailsO(n * m)O(n * m)Best general solution when you want simple normalization and fast uniqueness checks
Email Transformation with String Array ManipulationO(n * m)O(n * m)Useful when implementing manual parsing or minimizing repeated string operations

Video Solution

Unique Email Addresses - Two Solutions - Leetcode 929 Python • NeetCode • 26,093 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Unique Email Addresses easy or hard?
Unique Email Addresses is classified as an Easy problem on LeetCode. The challenge is understanding the transformation rules for the local part of the email and implementing them correctly. Once normalized, counting unique addresses with a hash set is straightforward.
Unique Email Addresses Python/Java solution
In Python, use a set and string methods like split(), replace(), and slicing to normalize the local name before adding it to the set. In Java, use HashSet<String> and String.split() or manual character iteration. Both implementations follow the same normalization rules and achieve O(n * m) complexity.
How to solve Unique Email Addresses in O(n)?
Treat the problem as normalization followed by deduplication. For each email, split around '@', remove dots from the local name, ignore characters after '+', and rebuild the address. Insert the normalized email into a hash set. Since each email is scanned once, the complexity becomes O(n * m), which is linear relative to total characters processed.
What is the best approach for Unique Email Addresses?
The best approach normalizes each email and stores the result in a hash set. Remove dots from the local name, ignore everything after the first plus sign, then combine it with the domain. Insert the normalized address into a set to ensure uniqueness. This method runs in O(n * m) time where n is the number of emails and m is the average email length.
Is Unique Email Addresses asked at Google/Amazon/Meta?
Unique Email Addresses is commonly seen in coding interview preparation lists and practice sets. It appears in screening rounds and practice platforms because it tests string manipulation and hash set usage. The difficulty is easy, but it checks whether candidates handle parsing rules carefully.
What data structure is used in Unique Email Addresses?
A hash set is the primary data structure. After normalizing each email address, the set stores unique entries and automatically removes duplicates. The approach relies on constant‑time average insertion and lookup provided by hash tables.
What is the time complexity of Unique Email Addresses?
The typical solution runs in O(n * m) time. Each email is processed once and each character in the email is examined during normalization. Space complexity is also O(n * m) because the set stores up to n normalized email strings.

Ready to solve this problem?

Practice Unique Email Addresses with our built-in code editor and test cases.

Practice on FleetCode