Spot a repeat
easy
New here? How this page works
- Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
- Open Break it down. Answer the questions in your own words. This is your plan.
- Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
- Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
- Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
- Press Submit. It runs hidden tests too. Pass them all, and it's solved.
A raffle ticket printer has a fault: now and then it prints the same number twice. Given the list of printed tickets, find out whether that happened.
Return true if any number appears more than once, and false if every number is different.
Examples
Example 1
- Input
- tickets = [4, 9, 2, 9]
- Output
- true
9 is printed twice.
Example 2
- Input
- tickets = [1, 2, 3]
- Output
- false
Every number is different.
Example 3
- Input
- tickets = []
- Output
- false
No tickets at all means no repeats.
Rules
ticketshas between 0 and 20,000 numbers.- Ticket numbers are whole numbers, and can be negative.
Words on this page, in plain English (2)Open if any word is new to you
- Boolean (true or false)
- A value that can only be
trueorfalse. Like a light switch: on or off. - Array (list)
- A list of values kept in order, inside square brackets. Like a row of numbered lockers.
Answer each question in your own words before you look at ours. Rough notes are fine: the point is to think before you type code. Your answers stay in this browser.
1.What goes in?
Name every input and what it looks like. Can it be empty? Sorted? Negative?
Stuck? Each hint gives a little more away. Try the problem again after each one.
This explains the whole solution.
You will learn the most by trying first, even if you don’t finish. But if this pattern is new to you, reading the walkthrough first is a fine way to learn it.
1. Understand the problem
We get a list of numbers. The only question is yes or no: does any number appear more than once? We don't need to say which number, or where.
An empty list, or a list with one ticket, can't have a repeat, so the answer there is false.
2. Solve an example by hand
Take [4, 9, 2, 9]. Reading left to right you'd think: 4, new. 9, new. 2, new. 9, wait, I've seen that one. Answer: true.
You were keeping a mental list of the numbers you'd already read. That's exactly what the program needs to do.
3. The slow way: compare every pair
Compare each ticket with every ticket after it:
function hasRepeat(tickets) { for (let i = 0; i < tickets.length; i++) { for (let j = i + 1; j < tickets.length; j++) { if (tickets[i] === tickets[j]) return true; } } return false;}Correct, but for 20,000 different tickets it makes about 200 million comparisons before it can say false. That's O(n²).
4. Spot the pattern
The question for each ticket is "have I seen this before?", the signal for hashing. We don't need counts or positions, just yes or no, so a Set is the perfect fit.
5. The faster idea
Go through the tickets once. For each one, ask the Set if it's there. If yes, we've found a repeat. If no, add it and move on. If we reach the end, there were no repeats.
Remember each number in a Set
tickets
seen (Set)
empty
function hasRepeat(tickets) { const seen = new Set(); for (const ticket of tickets) { if (seen.has(ticket)) { return true; } seen.add(ticket); } return false;}Start with an empty Set of numbers we have seen.
6. The code, line by line
function hasRepeat(tickets) { const seen = new Set();An empty Set to remember the numbers we've read so far.
for (const ticket of tickets) { if (seen.has(ticket)) { return true; }for...ofgives us each ticket in turn. If the Set already has it, we've seen it before: returntruestraight away. No need to read the rest.seen.add(ticket);First time we've seen this number: add it to the Set.
return false;If the loop finishes, every number was new, so the answer is
false.
7. How fast is it?
| Every pair | Set | |
|---|---|---|
| Time | O(n²) | O(n): one pass |
| Extra memory | O(1) | O(n): the Set |
There's a neat one-line version too: return new Set(tickets).size !== tickets.length;. A Set drops duplicates, so if it ends up smaller than the list, something repeated. It's also O(n), but it always reads the whole list, while the loop can stop early.
Common mistakes
- Returning
falseinside the loop. Only the end of the loop can know that nothing repeats. Putreturn falseafter it. - Sorting first and forgetting the cost. Sorting, then checking neighbours, works, but sorting takes O(n log n) time and changes the caller's list.
Memorise the solution, once you understand it.
This shows the full solution, then helps you rebuild it from memory in four steps. Remembering it is what lets you write it again in an interview, or in a week. Your result goes into your reviews, so it comes back just before you'd forget.
Learn it by heart: Spot a repeat
Read the 10 lines slowly. For each one, say out loud what it does and why it is there. Then cover it and move on to step 2.
function hasRepeat(tickets) { const seen = new Set(); for (const ticket of tickets) { if (seen.has(ticket)) { return true; } seen.add(ticket); } return false;}