Leetcode – 929. Unique Email Addresses

Lets quickly go into the solution for the unique email addresses problem

we can see every email in localName + @ + domainName this form

let localName = email.split(‘@’)[0] ?? ”;
const domainName = email.split(‘@’)[1];

And the question tells …


This content originally appeared on DEV Community and was authored by Rakesh Reddy Peddamallu

Lets quickly go into the solution for the unique email addresses problem

we can see every email in localName + @ + domainName this form

let localName = email.split('@')[0] ?? '';
const domainName = email.split('@')[1];

And the question tells if localName has + sign in it we need to ignore the rest of the characters after it , example "m.y+name@email.com" is equal to "m.y@email.com"

if (localName.includes('+')) {
    localName = localName.split('+')?.[0];
}

Also the question tells us that if we have . in email then it is considered as forwardable to the email , after replacing . (dot) with '' empty character

if (localName.includes('.') || domainName.includes('.')) {
    isForwardable = true;
    localName = localName.replace(/\./g, '');
  }

  if (isForwardable) {
    return localName + '@' + domainName; //returning email
  } else {
    return ''; //returning empty string if not forwardible
  }

Complete code

/**
 * @param {string[]} emails
 * @return {number}
 */


 const convertEmail = email => {
  let localName = email.split('@')[0] ?? '';
  const domainName = email.split('@')[1];

  let isForwardable = false;

  if (localName.includes('+')) {
    localName = localName.split('+')?.[0];
  }

  if (localName.includes('.') || domainName.includes('.')) {
    isForwardable = true;
    localName = localName.replace(/\./g, '');
  }

  if (isForwardable) {
    return localName + '@' + domainName;
  } else {
    return '';
  }
};
var numUniqueEmails = function(emails) {

    let uniqueEmails = new Set();
  emails.forEach(email => {
    const convertedEmail = convertEmail(email);
    if (convertedEmail !== '') {
      uniqueEmails.add(convertedEmail);
    }
  });
 return uniqueEmails.size;
};

Please do follow the series if you are struggling with leetcode questions 😇


This content originally appeared on DEV Community and was authored by Rakesh Reddy Peddamallu


Print Share Comment Cite Upload Translate Updates
APA

Rakesh Reddy Peddamallu | Sciencx (2024-06-24T03:51:41+00:00) Leetcode – 929. Unique Email Addresses. Retrieved from https://www.scien.cx/2024/06/24/leetcode-929-unique-email-addresses/

MLA
" » Leetcode – 929. Unique Email Addresses." Rakesh Reddy Peddamallu | Sciencx - Monday June 24, 2024, https://www.scien.cx/2024/06/24/leetcode-929-unique-email-addresses/
HARVARD
Rakesh Reddy Peddamallu | Sciencx Monday June 24, 2024 » Leetcode – 929. Unique Email Addresses., viewed ,<https://www.scien.cx/2024/06/24/leetcode-929-unique-email-addresses/>
VANCOUVER
Rakesh Reddy Peddamallu | Sciencx - » Leetcode – 929. Unique Email Addresses. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/24/leetcode-929-unique-email-addresses/
CHICAGO
" » Leetcode – 929. Unique Email Addresses." Rakesh Reddy Peddamallu | Sciencx - Accessed . https://www.scien.cx/2024/06/24/leetcode-929-unique-email-addresses/
IEEE
" » Leetcode – 929. Unique Email Addresses." Rakesh Reddy Peddamallu | Sciencx [Online]. Available: https://www.scien.cx/2024/06/24/leetcode-929-unique-email-addresses/. [Accessed: ]
rf:citation
» Leetcode – 929. Unique Email Addresses | Rakesh Reddy Peddamallu | Sciencx | https://www.scien.cx/2024/06/24/leetcode-929-unique-email-addresses/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.