Count unique domains from given List of Emails
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.
Input: arr[] = {“geeks@geeksforgeeks.org”, “google@gmail.com”}
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++
#include <bits/stdc++.h>
using namespace std;
vector<pair<string, int > > finddomains(
vector<string> input)
{
map<string, int > domainFre;
vector<pair<string, int > > ans;
for ( int i = 0; i < input.size(); i++) {
auto findindex = input[i].find( '@' );
domainFre[input[i].substr(findindex + 1)]++;
}
for ( auto it : domainFre)
ans.push_back({ it.first, it.second });
return ans;
}
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;
ans = finddomains(input);
for ( int i = 0; i < ans.size(); i++)
cout << ans[i].first << " "
<< ans[i].second << endl;
return 0;
}
|
Java
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 {
public static ArrayList<pair>
finddomains(String[] input)
{
TreeMap<String, Integer> domainFre
= new TreeMap<String, Integer>();
ArrayList<pair> ans = new ArrayList<>();
for ( int i = 0 ; i < input.length; i++) {
int findindex = input[i].indexOf( '@' );
String temp = input[i].substring(findindex + 1 );
if (domainFre.get(temp) != null ) {
domainFre.put(temp,
domainFre.get(temp) + 1 );
}
else {
domainFre.put(temp, 1 );
}
}
for (Map.Entry<String, Integer> it :
domainFre.entrySet())
ans.add( new pair(it.getKey(), it.getValue()));
return ans;
}
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" };
ArrayList<pair> ans = finddomains(input);
for ( int i = 0 ; i < ans.size(); i++)
System.out.println(ans.get(i).first + " "
+ ans.get(i).second);
}
}
|
Python3
def finddomains(input_):
domainFre = dict ()
ans = []
for i in range ( len (input_)):
findindex = input_[i].index( '@' )
if input_[i][findindex + 1 ::] in domainFre:
domainFre[input_[i][findindex + 1 ::]] + = 1
else :
domainFre[input_[i][findindex + 1 ::]] = 1
for it in domainFre:
ans.append([it, domainFre[it]])
ans.sort()
return ans
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 = []
ans = finddomains(input_)
for i in range ( len (ans)):
print (ans[i][ 0 ], ans[i][ 1 ])
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
public static List<pair> finddomains(String[] input)
{
SortedDictionary<String, int > domainFre = new SortedDictionary<String, int >();
List<pair> ans = new List<pair>();
for ( int i = 0 ; i < input.Length ; i++) {
int findindex = input[i].IndexOf( '@' );
String temp = input[i].Substring(findindex + 1);
if (domainFre.ContainsKey(temp)) {
domainFre[temp]+=1;
}
else {
domainFre.Add(temp, 1);
}
}
foreach (KeyValuePair<String, int > it in domainFre)
ans.Add( new pair(it.Key, it.Value));
return ans;
}
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"
};
List<pair> ans = finddomains(input);
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;
}
}
|
Javascript
<script>
const finddomains = (input) => {
let domainFre = {};
let ans = [];
for (let i = 0; i < input.length; i++) {
let findindex = input[i].indexOf( '@' );
if (input[i].substring(findindex + 1) in domainFre)
domainFre[input[i].substring(findindex + 1)]++;
else domainFre[input[i].substring(findindex + 1)] = 1;
}
for (let it in domainFre)
ans.push([it, domainFre[it]]);
return ans.sort();
}
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 = [];
ans = finddomains(input);
for (let i = 0; i < ans.length; i++)
document.write(`${ans[i][0]} ${ans[i][1]}<br/>`);
</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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...