# Count unique domains from given List of Emails

• Difficulty Level : Medium
• Last Updated : 16 Jun, 2022

Given an array arr[] containing N email addresses with different domains, the task is to find the unique domains and their frequencies from the list of emails.

Note:  The occurrence of the domain must be printed in the lexicographical order of domain names.

Examples:

Input: arr[] = { “rupesh@gmail.com”, “akole@yahoo.com”, “rupesh.21910879@viit.ac.in”,
“faculty.surname@viit.ac.in”, “Shyam@gmail.com”, “examcell@viit.ac.in”}
Output: gmail.com 2
viit.ac.in 3
yahoo.com 1
Explanation:  Here the unique domains in lexicographical order from the list of emails are:
gmail.com, viit.ac.in, yahoo.com.
And their respective frequency in the list is 2, 3 and 1.

Output: geeksforgeeks.org 1
gmail.com 1

Approach: The solution to the problem is based on the following idea:

The domains of an email are mentioned after the ‘@’ symbol. So, find all the domains and store their frequencies.

Follow the steps mentioned below to implement the idea:

• Initialize a map to store the frequencies of each unique domain in the lexicographical order of domain names.
• Transverse each of the email entries:
• For each entry find the index (say idx) of the ‘@’ symbol.
• The substring starting from idx+1 till the end of the string is the domain name.
• Store this domain in the map and increase its frequency by 1.
• Iterate the map from start (as the key are already in sorted order) and print the domain names and their frequencies.

Below is the implementation of the above approach.

## C++

 `// C++ code to implement the approach`   `#include ` `using` `namespace` `std;`   `// Function to find the unique domains` `// and their frequencies` `vector > finddomains(` `    ``vector input)` `{` `    ``// Map to store unique domains` `    ``// and their frequency` `    ``map domainFre;` `    ``vector > ans;`   `    ``// Loop to find the unique domains` `    ``for` `(``int` `i = 0; i < input.size(); i++) {`   `        ``// Find the index of '@' symbol in string` `        ``auto` `findindex = input[i].find(``'@'``);`   `        ``// Push the substring starting from` `        ``// findindex + 1 till the end` `        ``// and then increase its frequency by 1` `        ``domainFre[input[i].substr(findindex + 1)]++;` `    ``}`   `    ``// Store the key value pair into` `    ``// vector and finally return it.` `    ``for` `(``auto` `it : domainFre)` `        ``ans.push_back({ it.first, it.second });`   `    ``// Return the domains and their frequencies` `    ``return` `ans;` `}`   `// Driver code` `int` `main()` `{` `    ``vector input = {` `        ``"rupesh@gmail.com"``, ``"akole@yahoo.com"``,` `        ``"rupesh.21910879@viit.ac.in"``,` `        ``"faculty.surname@viit.ac.in"``,` `        ``"Shyam@gmail.com"``, ``"examcell@viit.ac.in"` `    ``};` `    ``vector > ans;`   `    ``// Function call` `    ``ans = finddomains(input);`   `    ``// Print the unique domains and` `    ``// their frequencies` `    ``// in lexicographical order` `    ``for` `(``int` `i = 0; i < ans.size(); i++)` `        ``cout << ans[i].first << ``" "` `             ``<< ans[i].second << endl;` `    ``return` `0;` `}`

## Java

 `// Java code to implement the approach` `import` `java.io.*;` `import` `java.util.*;`   `class` `pair {` `    ``String first;` `    ``int` `second;` `    ``pair(String first, ``int` `second)` `    ``{` `        ``this``.first = first;` `        ``this``.second = second;` `    ``}` `}`   `class` `GFG {` `    ``// Function to find the unique domains` `    ``// and their frequencies` `    ``public` `static` `ArrayList` `        ``finddomains(String[] input)` `    ``{` `        ``// Map to store unique domains` `        ``// and their frequency` `        ``TreeMap domainFre` `            ``= ``new` `TreeMap();` `        ``ArrayList ans = ``new` `ArrayList<>();`   `        ``// Loop to find the unique domains` `        ``for` `(``int` `i = ``0``; i < input.length; i++) {`   `            ``// Find the index of '@' symbol in string` `            ``int` `findindex = input[i].indexOf(``'@'``);`   `            ``// Push the substring starting from` `            ``// findindex + 1 till the end` `            ``// and then increase its frequency by 1` `            ``String temp = input[i].substring(findindex + ``1``);` `            ``if` `(domainFre.get(temp) != ``null``) {` `                ``domainFre.put(temp,` `                              ``domainFre.get(temp) + ``1``);` `            ``}` `            ``else` `{` `                ``domainFre.put(temp, ``1``);` `            ``}` `        ``}`   `        ``// Store the key value pair into` `        ``// vector and finally return it.` `        ``for` `(Map.Entry it :` `             ``domainFre.entrySet())` `            ``ans.add(``new` `pair(it.getKey(), it.getValue()));`   `        ``// Return the domains and their frequencies` `        ``return` `ans;` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``String input[] = { ``"rupesh@gmail.com"``,` `                           ``"akole@yahoo.com"``,` `                           ``"rupesh.21910879@viit.ac.in"``,` `                           ``"faculty.surname@viit.ac.in"``,` `                           ``"Shyam@gmail.com"``,` `                           ``"examcell@viit.ac.in"` `};`   `        ``// Function call` `        ``ArrayList ans = finddomains(input);`   `        ``// Print the unique domains and` `        ``// their frequencies` `        ``// in lexicographical order` `        ``for` `(``int` `i = ``0``; i < ans.size(); i++)` `            ``System.out.println(ans.get(i).first + ``" "` `                               ``+ ans.get(i).second);` `    ``}` `}`   `// This code is contributed by Rohit Pradhan`

## Python3

 `# Python3 code to implement the approach`   `# function to find the unique domains` `# and their frequencies` `def` `finddomains(input_):` `  `  `    ``# map to store unique domains` `    ``# and their frequency` `    ``domainFre ``=` `dict``()` `    ``ans ``=` `[]`   `    ``# loop to find the unique domains` `    ``for` `i ``in` `range``(``len``(input_)):` `        ``# find the index of '@' symbol in the string` `        ``findindex ``=` `input_[i].index(``'@'``)`   `        ``# Push the substring starting from` `        ``# findindex + 1 till the end` `        ``# and then increase its frequency by 1` `        ``if` `input_[i][findindex ``+` `1``::] ``in` `domainFre:` `            ``domainFre[input_[i][findindex ``+` `1``::]] ``+``=` `1` `        ``else``:` `            ``domainFre[input_[i][findindex ``+` `1``::]] ``=` `1`   `    ``# store the key value pair  into the` `    ``# ans list and finally return it` `    ``for` `it ``in` `domainFre:` `        ``ans.append([it, domainFre[it]])`   `    ``# sorting the ans list` `    ``ans.sort()` `    ``return` `ans`     `# Driver Code` `input_ ``=` `[``"rupesh@gmail.com"``, ``"akole@yahoo.com"``,` `          ``"rupesh.21910879@viit.ac.in"``,` `          ``"faculty.surname@viit.ac.in"``,` `          ``"Shyam@gmail.com"``, ``"examcell@viit.ac.in"``]` `ans ``=` `[]`   `# Function call` `ans ``=` `finddomains(input_)`   `# Print the unique domains and` `# their frequencies` `# in lexicographical order` `for` `i ``in` `range``(``len``(ans)):` `    ``print``(ans[i][``0``], ans[i][``1``])` `    `  `    `  `# This code is contributed by phasing17`

## C#

 `// C# program to implement above approach` `using` `System;` `using` `System.Collections;` `using` `System.Collections.Generic;`   `class` `GFG` `{`   `  ``// Function to find the unique domains` `  ``// and their frequencies` `  ``public` `static` `List finddomains(String[] input)` `  ``{` `    ``// Map to store unique domains` `    ``// and their frequency` `    ``SortedDictionary domainFre = ``new` `SortedDictionary();` `    ``List ans = ``new` `List();`   `    ``// Loop to find the unique domains` `    ``for` `(``int` `i = 0 ; i < input.Length ; i++) {`   `      ``// Find the index of '@' symbol in string` `      ``int` `findindex = input[i].IndexOf(``'@'``);`   `      ``// Push the substring starting from` `      ``// findindex + 1 till the end` `      ``// and then increase its frequency by 1` `      ``String temp = input[i].Substring(findindex + 1);` `      ``if` `(domainFre.ContainsKey(temp)) {` `        ``domainFre[temp]+=1;` `      ``}` `      ``else` `{` `        ``domainFre.Add(temp, 1);` `      ``}` `    ``}`   `    ``// Store the key value pair into` `    ``// vector and finally return it.` `    ``foreach` `(KeyValuePair it ``in` `domainFre)` `      ``ans.Add(``new` `pair(it.Key, it.Value));`   `    ``// Return the domains and their frequencies` `    ``return` `ans;` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `Main(``string``[] args){`   `    ``String[] input = ``new` `String[]{` `      ``"rupesh@gmail.com"``,` `      ``"akole@yahoo.com"``,` `      ``"rupesh.21910879@viit.ac.in"``,` `      ``"faculty.surname@viit.ac.in"``,` `      ``"Shyam@gmail.com"``,` `      ``"examcell@viit.ac.in"` `      ``};`   `    ``// Function call` `    ``List ans = finddomains(input);`   `    ``// Print the unique domains and` `    ``// their frequencies` `    ``// in lexicographical order` `    ``for` `(``int` `i = 0 ; i < ans.Count ; i++){` `      ``Console.WriteLine(ans[i].first + ``" "` `+ ans[i].second);` `    ``}`   `  ``}` `}`   `public` `class` `pair{` `  ``public` `String first;` `  ``public` `int` `second;` `  ``public` `pair(String first, ``int` `second)` `  ``{` `    ``this``.first = first;` `    ``this``.second = second;` `  ``}` `}`   `// This code is contributed by subhamgoyal2014.`

## Javascript

 ``

Output

```gmail.com 2
viit.ac.in 3
yahoo.com 1```

Time Complexity: O(N * M) where M is the average string size
Auxiliary Space: O(K) where K is the number of unique domains

My Personal Notes arrow_drop_up
Related Articles