(LeetCode:数据库) 查找重复的电子邮箱
题目描述:编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。
+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+
根据以上输入,你的查询应返回以下结果:
+---------+
| Email |
+---------+
| a@b.com |
+---------+
说明:所有电子邮箱都是小写字母。
MySQL方案:
SELECT
email
FROM
person
GROUP BY
email
HAVING
count(email) > 1;
如果使用MySQL可以轻松解决这个问题,现在用C语言解决这个问题。
C语言方案:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
int len;
char *str[] = {
"[email protected]",
"[email protected]",
"[email protected]"
};
len = sizeof(str) / sizeof(*str);//求字符串元素个数
int i, j, k, l;
char *r[10] = { NULL };//字符串标记,存放重复的字符串
l = 0;//r的下标
for (i = 0; i < len; i++)
{
for (j = i + 1; j < strlen(*str); ++j)
{
if (str[i] == str[j])
{
for (k = 0; k < l; k++)
{
if (!(strcmp(str[i] , r[k])))
{
break;
}
}
if (k == l)
r[l++] = str[i];
}
}
}
for (int i = 0; r[i]!=NULL; i++)
printf("%s\n", r[i]);
}