王廷瑋|數位醫療|智慧醫療: 182. Duplicate Emails WFU

2024年7月11日 星期四

182. Duplicate Emails

182. Duplicate Emails


人員表

+-------------+---------+

| 列名 | 類型 |

+-------------+---------+

| id | int | | email | varchar |

+-------------+---------+

id 是這個表的主鍵(具有唯一值的列)。 這個表的每一行包含一個電子郵件地址。電子郵件地址不包含大寫字母。
解題說明

撰寫一個解決方案來報告所有重複的電子郵件地址。注意,保證 email 字段不為 NULL。

可以按任意順序返回結果表。

範例:

輸入:

人員表:

+----+---------+

| id | email |

+----+---------+

| 1 | a@b.com |

| 2 | c@d.com |

| 3 | a@b.com |

+----+---------+

輸出:

+---------+

| Email |

+---------+

| a@b.com |

+---------+

解釋: a@b.com 重複了兩次。


MySQL


# Write your MySQL query statement below

SELECT email AS Email
FROM Person
GROUP BY email
HAVING COUNT(email) > 1;

340ms


Pandas


import pandas as pd

def duplicate_emails(person: pd.DataFrame) -> pd.DataFrame:
# Group by email and count occurrences
email_counts = person.groupby('email').size().reset_index(name='count')
# Filter for emails that appear more than once
duplicates = email_counts[email_counts['count'] > 1]
# Select only the email column for the result
result_df = duplicates[['email']].rename(columns={'email': 'Email'})
return result_df

66.85MB, 370ms


PostgreSQL


SELECT email AS Email
FROM Person
GROUP BY email
HAVING COUNT(email) > 1;

228ms