-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniqueEmail.java
More file actions
40 lines (38 loc) · 1.27 KB
/
UniqueEmail.java
File metadata and controls
40 lines (38 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
public class UniqueEmail {
public static void main(String args[]){
Scanner input=new Scanner(System.in);
String s=input.nextLine();
String arr[]=s.split(" ");
int n=countEmail(arr);
System.out.println("Number of unique emails is= "+n);
}
public static int countEmail(String[] arr) {
HashSet<String> count=new HashSet<>();
String temp="";
for(String email:arr){
String firstPart=email.substring(0,email.indexOf('@'));
String secondPart=email.substring(email.indexOf('@'));
if(firstPart.contains("+")){
firstPart=firstPart.substring(0,firstPart.indexOf("+"));
}
for(int i=0;i<firstPart.length();i++){
char ch=firstPart.charAt(i);
if(ch!='.'){
temp+=ch;
}
}
firstPart=temp;
count.add(firstPart+secondPart);
temp="";
}
Iterator<String> itr=count.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
return count.size();
}
}