Skip to the code
+10 XP

#70 Find the Missing Ticket

easy

A raffle printed tickets numbered 0 to n, one of each, and sold all but one. The n sold ticket numbers are in tickets, in any order.

Write missingTicket(tickets) that returns the unsold ticket number. Try to solve it in O(n) time with O(1) extra memory.

Example 1

Input: tickets = [4,2,0,1]
Output: 3

Tickets go from 0 to 4, and 3 is not in the list.

Example 2

Input: tickets = [0]
Output: 1

Tickets 0 and 1 were printed, and only 0 was sold.

Constraints

  • 1 <= tickets.length <= 5 * 10^5
  • The numbers are distinct and come from 0 to tickets.length.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).