Shouldn't we use Union instead of Union all?


  • 10
    F

    I think the answer missed the case when A send B a friend request, and B send A a friend request, and both requests got approved. In this case, A or B really just gained one friend. But the answer seems to count this case twice.
    Isn't union (remove duplicates) should be used instead of union all?

    select id1 as id, count(id2) as num
    from
    (select requester_id as id1, accepter_id as id2 
    from request_accepted
    union
    select accepter_id as id1, requester_id as id2 
    from request_accepted) tmp1
    group by id1 
    order by num desc limit 1
    

  • 1
    Z

    @Fangyyfrank I agree. The solution is wrong. Hope they can fix this soon.


  • 0
    Q

    @Fangyyfrank Use "UNION ALL". In Friend Requests I, it says it's possible that one user send multiple requests to another and get accepted multiple times. But you idea to use UNION is correct.


  • 0
    Z

    @Fangyyfrank Agree. Even looking through the test cases of OJ, union all is clearly wrong. There is mutual friend request and repeated friend request.


  • 0
    N

    @Fangyyfrank said in Shouldn't we use Union instead of Union all?:

    select requester_id as id1, accepter_id as id2
    from request_accepted
    union
    select accepter_id as id1, requester_id as id2
    from request_accepted

    AGREED


  • 0
    E

    @Fangyyfrank Nice Thinking.
    I noticed the note in the problem "The friend request could only been accepted once". Doesn't it mean when the case "A send B a friend request, and B send A a friend request" occurs, only one of A and B can accept it?

    Like of course they can send friend request to each other, but once one of them accepts it (it will be recorded in the table) the other person won't be able to accept it again(It won't write this record to the table second time with same id pair). It is just like how Facebook works.

    Please correct me if I am wrong, thanks.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.