LeetCode 182. Duplicate Emails

Tutd Schema:

person :: {id Integer, email Text};
person := relation{tuple{id 1, email "a@b.com"},
                  tuple{id 2, email "b@c.com"},
                  tuple{id 3, email "a@b.com"}};

 Thinking process:

 TutorialD (master/main): :showexpr (person group ({id} as ids)) where ^gt(count(@ids),1)
┌───────────┬───────────────────────────┐
│email::Text│ids::relation {id::Integer}│
├───────────┼───────────────────────────┤
│"a@b.com"  │┌───────────┐              │
│           ││id::Integer│              │
│           │├───────────┤              │
│           ││1          │              │
│           ││3          │              │
│           │└───────────┘              │
└───────────┴───────────────────────────┘

Bonus1: Find ids that should be kept.

TutorialD (master/main): minIds := (person group ({all but email} as ids):{minId := min(@ids)}){all but ids}
TutorialD (master/main): :showexpr minIds
┌───────────┬──────────────┐
│email::Text│minId::Integer│
├───────────┼──────────────┤
│"a@b.com"  │1             │
│"b@c.com"  │2             │
└───────────┴──────────────┘

Bonus2: Find ids that should be deleted.

TutorialD (master/main): redundantIds := person not matching (minIds rename {minId as id})
TutorialD (master/main): :showexpr redundantIds
┌───────────┬───────────┐
│email::Text│id::Integer│
├───────────┼───────────┤
│"a@b.com"  │3          │
└───────────┴───────────┘


留言

這個網誌中的熱門文章

LeetCode 184. Department Highest Salary