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]`. Master advanced TypeScript type manipulation in this medium-level challenge on TypeScriptPro.
In this medium-level challenge, you'll implement FindEles<T> which filters a tuple to only include elements that appear exactly once in the array.
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].
Change the following code to make the test cases pass (no type check errors).
type Includes<T extends any[], V> = T extends [infer First, ...infer Rest]
? Equal<First, V> extends true
? true
: Includes<Rest, V>
: false
type FindEles<T extends any[], Seen extends any[] = []> = T extends [infer First, ...infer Rest]
? Includes<Rest, First> extends true
? FindEles<Rest, [...Seen, First]>
: Includes<Seen, First> extends true
? FindEles<Rest, Seen>
: [First, ...FindEles<Rest, Seen>]
: []How it works:
Includes<T, V> is a helper type that checks whether a value V exists in a tuple T using strict Equal comparisonsFindEles<T, Seen> processes the tuple element by element, maintaining a Seen accumulator of elements that have already been encountered as duplicatesFirst, it first checks if the same element appears later in Rest -- if so, the element is a duplicate, so it skips it and adds it to SeenSeen before, it is also skipped since it was previously identified as a duplicate[First, ...FindEles<Rest, Seen>][] when all elements have been processedThis challenge helps you understand recursive tuple filtering with auxiliary tracking state, and how to apply these concepts in real-world scenarios.
This challenge is originally from here.
Be the first to access the course, unlock exclusive launch bonuses, and get special early-bird pricing before anyone else.
Only 27 Spots left
Get 1 month early access
Pre-Launch discount