Concise MySQL solution with reference


  • 2
    Z
    # Refer to: https://stackoverflow.com/questions/17250243/how-to-return-null-when-result-is-empty
    select(
      select num
      from number
      group by num
      having count(*) = 1
      order by num desc limit 1
    ) as num;
    

  • 0

    Same idea

    select max(a.num) as num
    from (select num from number group by num having count(*)=1) as a

  • 0
    Z

    Actually the first solution is much faster than the solution above. They are different. The second one uses a derived table while the first one does not. the first one beats about 85% but the second one only beats about 45%. BTW, it is a very clever idea to transform empty values to NULL using the first solution.


Log in to reply
 

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