Find lexicographically first ID of each domain from given Array of email IDs
Last Updated :
28 Feb, 2023
You are given an array of Email IDs, the task is to return the lexicographically first email ID of every single domain.
Examples:
Input: Emails[] = {“ajay@gmail.com”, “rahul@hotmail.com”, “saif@gmail.com”, “faheem@yahoo.com”, “sam@hotmail.com”}
Output: {“ajay@gmail.com”, “rahul@hotmail.com”, “faheem@yahoo.com”}
Explanation: All email IDs are of the total of 3 email domains in the input array so the output contains alphabetically the first email ID of every domain.
Input: Emails[] = {“ben@gmail.com”, “alex@gmail.com”, “fin@gmail.com”, “zeke@gmail.com”}
Output: {“alex@gmail.com”}
Explanation: Every email ID is of only one domain i.e. “gmail” so the email which is alphabetically first among all is returned.
Approach: Implement the idea below to solve the problem:
The idea is to sort the input emails first and then iterate over the input email. For every single email extract its domain name and map it with entire email address using a hashmap and while iterating check whether any email of current email Id’s domain is present or not. If no such email Id is present in hashmap then insert the current email Id.
Follow the steps mentioned below to implement the idea:
- Sort the Input Array and declare a hashmap that will have a string as key and value.
- Iterate over the array and check whether the current email’s domain is present in the hashmap or not.
- If yes, then do nothing.
- Otherwise, insert the current email ID into the hashmap along with its domain name.
- Iterate over the Hashmap and populate the output array with its values.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void sendMail(string Emails[], int n)
{
map<string, string> mp;
sort(Emails, Emails + n);
for ( int i = 0; i < n; i++) {
string temp = Emails[i];
int place = temp.find( '@' );
string domain = temp.substr(place);
if (!mp.count(domain)) {
mp[domain] = temp;
}
}
for ( auto it = mp.begin(); it != mp.end(); it++) {
cout << it->second << endl;
}
}
int main()
{
int N = 5;
string Emails[]
= { "ajay@gmail.com" , "rahul@hotmail.com" ,
"saif@gmail.com" , "faheem@yahoo.com" ,
"sam@hotmail.com" };
sendMail(Emails, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void sendMail(String[] Emails, int n)
{
HashMap<String, String> mp = new HashMap<>();
Arrays.sort(Emails);
for ( int i = 0 ; i < n; i++) {
String temp = Emails[i];
int place = temp.indexOf( '@' );
String domain = temp.substring(place);
if (!mp.containsKey(domain)) {
mp.put(domain, temp);
}
}
for (String it : mp.values()) {
System.out.println(it);
}
}
public static void main(String[] args)
{
int N = 5 ;
String[] Emails
= { "ajay@gmail.com" , "saif@gmail.com" ,
"rahul@hotmail.com" , "faheem@yahoo.com" ,
"sam@hotmail.com" };
sendMail(Emails, N);
}
}
|
Python3
def sendMail(Emails, n):
Emails.sort()
mp = {}
for i in range (n):
temp = Emails[i]
place = temp.find( '@' )
domain = temp[place:]
if domain not in mp:
mp[domain] = temp
for i in mp:
print (mp[i])
if __name__ = = '__main__' :
N = 5
Emails = [ "ajay@gmail.com" , "rahul@hotmail.com" ,
"saif@gmail.com" , "faheem@yahoo.com" ,
"sam@hotmail.com" ]
sendMail(Emails, N)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void sendMail(String[] Emails, int n)
{
Dictionary<String, String> mp
= new Dictionary<String, String>();
Array.Sort(Emails);
for ( int i = 0; i < n; i++) {
String temp = Emails[i];
int place = temp.IndexOf( '@' );
String domain = temp.Substring(place);
if (!mp.ContainsKey(domain)) {
mp.Add(domain, temp);
}
}
foreach (String it in mp.Values)
{
Console.WriteLine(it);
}
}
public static void Main(String[] args)
{
int N = 5;
String[] Emails
= { "ajay@gmail.com" , "saif@gmail.com" ,
"rahul@hotmail.com" , "faheem@yahoo.com" ,
"sam@hotmail.com" };
sendMail(Emails, N);
}
}
|
Javascript
const sendMail = (Emails, n) => {
let mp = {};
Emails.sort((a, b) => a - b)
for (let i = 0; i < n; i++) {
let temp = Emails[i];
let place = temp.indexOf( '@' );
let domain = temp.substring(place);
if (!(domain in mp)) {
mp[domain] = temp;
}
}
for (let it in mp) {
console.log(`${mp[it]}<br/>`);
}
}
let N = 5;
let Emails
= [ "ajay@gmail.com" , "rahul@hotmail.com" ,
"saif@gmail.com" , "faheem@yahoo.com" ,
"sam@hotmail.com" ];
sendMail(Emails, N);
|
Output
ajay@gmail.com
rahul@hotmail.com
faheem@yahoo.com
Time Complexity: O(N LOG N)
Auxiliary Space: O(N)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...