Count distinct emails present in a given array | C ++ program for the above approach ; Function to count all the distinct emails after preprocessing according to the given rules ; Traverse the given array of strings arr [ ] ; Stores the position of ' @ ' in the string ; If pos2 < x . size ( ) ; Erases all the occurrences of ' . ' before pos2 ; Stores the position of the first ' + ' ; Update the position pos2 ; If ' + ' exists then erase characters after ' + ' and before ' @ ' ; Insert all the updated strings inside the set ; Return the size of set ans ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int distinctEmails ( vector < string > & emails ) { for ( auto & x : emails ) { auto pos2 = x . find ( ' @ ' ) ; if ( pos2 < x . size ( ) ) x . erase ( remove ( x . begin ( ) , x . begin ( ) + pos2 , ' . ' ) , x . begin ( ) + pos2 ) ; auto pos1 = x . find ( ' + ' ) ; pos2 = x . find ( ' @ ' ) ; if ( pos1 < x . size ( ) and pos2 < x . size ( ) ) { x . erase ( pos1 , pos2 - pos1 ) ; } } unordered_set < string > ans ( emails . begin ( ) , emails . end ( ) ) ; return ans . size ( ) ; } int main ( ) { vector < string > arr = { " raghav . agg @ geeksforgeeks . com " , " raghavagg @ geeksforgeeks . com " } ; cout << distinctEmails ( arr ) ; return 0 ; }