(LeetCode: database) find duplicate e-mail
Topic Description: Writing a SQL query to find all duplicate Person table e-mail.
+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+
Based on the above inputs, your query should return the following results:
+---------+
| Email |
+---------+
| a@b.com |
+---------+
Note: All e-mail all lowercase letters.
MySQL programs:
SELECT
email
FROM
person
GROUP BY
email
HAVING
count(email) > 1;
If you are using MySQL can easily solve this problem, and now to solve this problem in C language.
C language program:
#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]);
}