Find lexicographically first ID of each domain from given Array of email IDs
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++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to get the alphabetically first // email id of each domain void sendMail(string Emails[], int n) { map<string, string> mp; // Sorting input array sort(Emails, Emails + n); for ( int i = 0; i < n; i++) { string temp = Emails[i]; // Finding index of @ so that we can // extract the domain of email as // substring using this index int place = temp.find( '@' ); // Storing domain of email address string domain = temp.substr(place); // Checking whether we already have // an email of this domain or not if (!mp.count(domain)) { mp[domain] = temp; } } // Iterating over map to get the desired // output for ( auto it = mp.begin(); it != mp.end(); it++) { cout << it->second << endl; } } // Driver Code int main() { int N = 5; string Emails[] = { "ajay@gmail.com" , "rahul@hotmail.com" , "saif@gmail.com" , "faheem@yahoo.com" , "sam@hotmail.com" }; // Function call sendMail(Emails, N); return 0; } |
Java
// Java code to implement the approach import java.io.*; import java.util.*; class GFG { // Function to get the alphabetically first // email id of each domain static void sendMail(String[] Emails, int n) { HashMap<String, String> mp = new HashMap<>(); // Sorting input array Arrays.sort(Emails); for ( int i = 0 ; i < n; i++) { String temp = Emails[i]; // Finding index of @ so that we can extract the // domain of email as substring using this index int place = temp.indexOf( '@' ); // Storing domain of email address String domain = temp.substring(place); // Checking whether we already have // an email of this domain or not if (!mp.containsKey(domain)) { mp.put(domain, temp); } } // Iterating over map to get the desired // output 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" }; // Function call sendMail(Emails, N); } } // This code is contributed by lokeshmvs21. |
Python3
# Python code to implement the approach # Function to get the alphabetically first # email id of each domain def sendMail(Emails, n): # Sorting input array Emails.sort() mp = {} for i in range (n): temp = Emails[i] # Finding index of @ so that we can # extract the domain of email as # substring using this index place = temp.find( '@' ) # Storing domain of email address domain = temp[place:] # Checking whether we already have # an email of this domain or not if domain not in mp: mp[domain] = temp # Iterating over map to get the desired # output for i in mp: print (mp[i]) # Driver Code if __name__ = = '__main__' : N = 5 Emails = [ "ajay@gmail.com" , "rahul@hotmail.com" , "saif@gmail.com" , "faheem@yahoo.com" , "sam@hotmail.com" ] # Function call sendMail(Emails, N) # This code is contributed by Tapesh(tapeshdua420) |
C#
// C# code to implement the approach using System; using System.Collections.Generic; class GFG { // Function to get the alphabetically first // email id of each domain static void sendMail(String[] Emails, int n) { Dictionary<String, String> mp = new Dictionary<String, String>(); // Sorting input array Array.Sort(Emails); for ( int i = 0; i < n; i++) { String temp = Emails[i]; // Finding index of @ so that we can extract the // domain of email as substring using this index int place = temp.IndexOf( '@' ); // Storing domain of email address String domain = temp.Substring(place); // Checking whether we already have // an email of this domain or not if (!mp.ContainsKey(domain)) { mp.Add(domain, temp); } } // Iterating over map to get the desired // output 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" }; // Function call sendMail(Emails, N); } } // This code is contributed by Tapesh(tapeshdua420) |
Javascript
// JavaScript code to implement the approach // Function to get the alphabetically first // email id of each domain const sendMail = (Emails, n) => { let mp = {}; // Sorting input array Emails.sort((a, b) => a - b) for (let i = 0; i < n; i++) { let temp = Emails[i]; // Finding index of @ so that we can // extract the domain of email as // substring using this index let place = temp.indexOf( '@' ); // document.write(temp, place, temp.substring(place)) // Storing domain of email address let domain = temp.substring(place); // Checking whether we already have // an email of this domain or not if (!(domain in mp)) { mp[domain] = temp; } } // Iterating over map to get the desired // output for (let it in mp) { console.log(`${mp[it]}<br/>`); } } // Driver Code let N = 5; let Emails = [ "ajay@gmail.com" , "rahul@hotmail.com" , "saif@gmail.com" , "faheem@yahoo.com" , "sam@hotmail.com" ]; // Function call sendMail(Emails, N); // This code is contributed by rakeshsahni |
ajay@gmail.com rahul@hotmail.com faheem@yahoo.com
Time Complexity: O(N)
Auxiliary Space: O(N)
Related Articles:
- Introduction to Arrays – Data Structures and Algorithms
- Introduction to Hashing – Data Structures and Algorithms Tutorials
Please Login to comment...