복's

[ LeetCode - 182 ] Duplicate Emails 본문

알고리즘/LeetCode

[ LeetCode - 182 ] Duplicate Emails

나복이 2023. 12. 1. 22:46
728x90

https://leetcode.com/problems/duplicate-emails/description/

 

Duplicate Emails - LeetCode

Can you solve this real interview question? Duplicate Emails - Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | email | varchar | +-------------+---------+ id is the primary key (column with unique val

leetcode.com

중복되는 email 컬럼이 있는 경우만 출력하면 되는 문제로 2가지의 풀이로 풀어봤다.

문제가 너무 빨리 풀려서 다른 풀이로도 풀었다.


[ 📌 풀이 ]

1)  group by를 이용해서 email 별로 묶고 count를 통해서 개수를 확인해서 1개 이상인 경우 출력 하기

2) 조인을 통해서 테이블을 붙인 다음에 email은 같으나 id가 다른 row를 출력하기


[ 📌 코드 풀이1 - Oracle ]

-- Author    : Lee In Bok 
-- Date      : 2023.12.01(Fri)
-- Spend Time: 01m 32s
-- Runtime   : 855 ms (Beats 61.66%)
-- Algoritm  : Distinct, Aggregattion
 select email as Email
   from Person
  group by email
 having count(email) > 1

[ 📌 코드 풀이2 - Oracle ]

 select distinct p1.email AS Email
   from Person p1
  inner join Person p2
     on p1.email = p2.email
    and p1.id <> p2.id

[ 📌 결과 - Oracle ]

[ Result - Oracle ]

728x90