Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmCount unique domains from given List of Emails

Count unique domains from given List of Emails

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.

Input: arr[] = {“neveropen@neveropen.co.za”, “google@gmail.com”}
Output: neveropen.co.za 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 <bits/stdc++.h>
using namespace std;
 
// Function to find the unique domains
// and their frequencies
vector<pair<string, int> > finddomains(
    vector<string> input)
{
    // Map to store unique domains
    // and their frequency
    map<string, int> domainFre;
    vector<pair<string, int> > 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<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"
    };
    vector<pair<string, int> > 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<pair>
        finddomains(String[] input)
    {
        // Map to store unique domains
        // and their frequency
        TreeMap<String, Integer> domainFre
            = new TreeMap<String, Integer>();
        ArrayList<pair> 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<String, Integer> 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<pair> 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<pair> finddomains(String[] input)
  {
    // Map to store unique domains
    // and their frequency
    SortedDictionary<String, int> domainFre = new SortedDictionary<String, int>();
    List<pair> ans = new List<pair>();
 
    // 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<String, int> 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<pair> 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




<script>
    // JavaScript code to implement the approach
 
    // Function to find the unique domains
    // and their frequencies
    const finddomains = (input) => {
     
        // Map to store unique domains
        // and their frequency
        let domainFre = {};
        let ans = [];
 
        // Loop to find the unique domains
        for (let i = 0; i < input.length; i++) {
 
            // Find the index of '@' symbol in string
            let findindex = input[i].indexOf('@');
 
            // Push the substring starting from
            // findindex + 1 till the end
            // and then increase its frequency by 1
            if (input[i].substring(findindex + 1) in domainFre)
                domainFre[input[i].substring(findindex + 1)]++;
            else domainFre[input[i].substring(findindex + 1)] = 1;
        }
 
        // Store the key value pair into
        // vector and finally return it.
        for (let it in domainFre)
            ans.push([it, domainFre[it]]);
 
        // Return the domains and their frequencies
 
        return ans.sort();
    }
 
    // Driver code
 
    let input = [
        "rupesh@gmail.com", "akole@yahoo.com",
        "rupesh.21910879@viit.ac.in",
        "faculty.surname@viit.ac.in",
        "Shyam@gmail.com", "examcell@viit.ac.in"
    ];
    let ans = [];
 
    // Function call
    ans = finddomains(input);
 
    // Print the unique domains and
    // their frequencies
    // in lexicographical order
    for (let i = 0; i < ans.length; i++)
        document.write(`${ans[i][0]} ${ans[i][1]}<br/>`);
 
// This code is contributed by rakeshsahni
 
</script>


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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments