Appear Only Once
Less than 1 minuteTC-Medium
Appear Only Once
Problem
Find the elements in the target array that appear only once. For example: input: [1,2,2,3,3,4,5,6,6,6], output: [1,4,5].
Solution
Approach: Check Both Prefix and Suffix
For each element, verify it doesn't appear in the rest of the array, and also not before it.
type IsEqual<A, B> =
(<T>() => T extends A ? 1 : 2) extends
(<T>() => T extends B ? 1 : 2)
? true : false
type Includes<T extends unknown[], U> =
T extends [infer Head, ...infer Tail]
? IsEqual<Head, U> extends true
? true
: Includes<Tail, U>
: false
type AppearOnlyOnce<
T extends unknown[],
Seen extends unknown[] = []
> = T extends [infer Head, ...infer Tail]
? Includes<Seen, Head> extends true
? AppearOnlyOnce<Tail, Seen> // skip — already seen
: Includes<Tail, Head> extends true
? AppearOnlyOnce<Tail, [...Seen, Head]> // appears later — skip but mark seen
: [Head, ...AppearOnlyOnce<Tail, [...Seen, Head]>] // unique — include
: []How it works:
Seentracks elements already encountered.- If
Headis inSeen, skip it (it was a duplicate we already dropped). - If
Headappears inTail, mark it as seen and skip it. - Otherwise,
Headappears exactly once — include it in the result.
Key Takeaways
- A
Seenaccumulator prevents re-processing the same value twice in the result. - The two-phase check (in Seen? in Tail?) correctly handles elements appearing 3+ times.
IsEqualensures strict equality, avoiding issues withany/never.
