05117-medium-without

Back

// 你的答案
_**```
type ArrayToUnion<T> = T extends any[] ? T[number] : T;

type Without<
  T extends any[],
  U extends number | any[],
  Swap extends any[] = []
> = T extends [infer F, ...infer Rest]
  ? F extends ArrayToUnion<U>
    ? Without<Rest, U, Swap>
    : Without<Rest, U, [...Swap, F]>
  : Swap;
```**_

Solution by shx123qwe #35118

type Without<T, U, A extends any[] = []> = T extends [infer F, ...infer Rest]
  ? F extends (U extends any[] ? U[number] : U)
    ? Without<Rest, U, A>
    : Without<Rest, U, [...A, F]>
  : A;

Solution by wendao-liu #35072

type Without<T, U> = [U] extends [(infer R)[] | infer R]
  ? T extends [infer F, ...infer L]
    ? F extends R
      ? [...Without<L, R>]
      : [F, ...Without<L, R>]
    : T
  : T;

Solution by holger2138 #34737

type ToUnion<T> = T extends any[] ? T[number] : T
type Without<T, U> = T extends [infer R, ...infer rest] ? R extends ToUnion<U> ? Without<rest, U> : [R, ...Without<rest, U>] : T

Solution by ouzexi #34090

type Without<T extends any[], U, A extends any[]=[]> = T extends [infer S, ...infer E] ? S extends (U extends (infer K)[] ? K : U) ? Without<E,U,A> : Without<E, U, [...A, S]>:A

Solution by rookiewxy #33858

// your answers
type ToUnion<U> = U extends unknown[]? U[number]: U
type Without<T extends any[], U> = T extends [infer S,...infer R]?
 S extends ToUnion<U> ?
  Without<R ,U> : [S,...Without<R, U>] :[]

Solution by pea-sys #33093

type ConvertToArray<T extends unknown | unknown[]> = T extends unknown[] ? T : [T]

type Without<T extends unknown[], U> = T extends [infer H, ...infer R]
  ? H extends ConvertToArray<U>[number]
    ? Without<R, U>
    : [H, ...Without<R, U>]
  : T;```

Solution by keyurparalkar #32512

type ToUnion<T> = T extends any[] ? T[number] : T;

type Without<T extends number[], U extends number | number[], V extends number[] = []> = T extends [
	infer First,
	...infer Rest,
]
	? First extends ToUnion<U>
		? Without<Rest extends number[] ? Rest : never, U, [...V]>
		: Without<Rest extends number[] ? Rest : never, U, [...V, First & number]>
	: V;

Solution by gasmg #32233

type Without<T, U, S = U extends number[] ? U[number]:U> = T extends [infer F, ...infer Rest]  ? F extends S ? Without<Rest, U, S>: [F, ...Without<Rest, U, S>]:T

Solution by Zhen-code #32025

type Without<T, U> = 
  T extends [infer F, ...infer R] ? F extends (U extends unknown[] ? U[number] : U) ? Without<R, U> : [F, ...Without<R, U>] : [];

Solution by preventdefault #31843

type ReturnArray<T> = T extends unknown[] ? T : [T];

type Without<T, U extends unknown[] | number> = T extends [infer F, ...infer Rest]
  ? F extends ReturnArray<U>[number]
    ? Without<Rest, U>
    : [F, ...Without<Rest, U>]
  : [];

Solution by leejaehyup #30848

// Removes an element from an array
type WithoutEle<TArr extends unknown[], UEle> = TArr extends []
  ? []
  : TArr extends [infer TEle, ...infer TRest]
    ? Equal<TEle, UEle> extends true
      ? WithoutEle<TRest, UEle>
      : [TEle, ...WithoutEle<TRest, UEle>]
    : never;

// Removes an element or an array of elements from an array
type Without<TArr extends unknown[], U> = U extends []
  ? TArr
  : U extends [infer UEle, ...infer URest]
    ? Without<WithoutEle<TArr, UEle>, URest>
    : WithoutEle<TArr, U>;

Solution by olalonde #30260

type Without<
  T extends any[],
  U extends T[number] | T[number][],
  V extends T[number] = U extends T[number][] ? U[number] : U
> =
  T extends [infer F, ...infer R]
    ? [...(F extends V ? [] : [F]), ...Without<R, never, V>]
    : []

Solution by sbr61 #30213

type Without<T extends any[], E extends any[], R extends any[] = []> = T extends [infer A, ...infer B] ?
    A extends E[number] ?
        Without<B, E, [...R]>:
        Without<B, E, [...R, A]>:
    R;

type without = Without<[1, 2, 3], [1, 2, 3]>;

const wo: without = [];

Solution by sundial-dreams #29546

// your answers

type ToUnion<U> = U extends unknown[]? U[number]: U
type Without<T , U> = 
          T extends [infer First , ...infer Rest] ?
                     First extends ToUnion<U> ?
                         Without<Rest ,U> : 
                          [First , ...Without<Rest, U>] 
                      : []

Solution by kerolossamir165 #29295

type Without<T, U> = U extends any[]
  ? T extends [infer Head, ...infer Tail]
    ? Head extends U[number]
      ? [...Without<Tail, U>]
      : [Head, ...Without<Tail, U>]
    : T
  : T extends [infer Head, ...infer Tail]
  ? Head extends U
    ? [...Without<Tail, U>]
    : [Head, ...Without<Tail, U>]
  : T

Solution by Kying-star #29039

type ToUnion<T extends number | number[]> = T extends number[] ? T[number] : T;

type Without<T, U extends number | number[], Union = ToUnion<U>> = T extends [
  infer F,
  ...infer Rest
]
  ? F extends Union
    ? [...Without<Rest, U, Union>]
    : [F, ...Without<Rest, U, Union>]
  : [];

Solution by DoubleWoodLin #28777

type Union<T> = T extends any[] ? T[number] : T
type Without<T, U> = T extends [infer First, ...infer Rest]
  ? First extends Union<U>
    ? [...Without<Rest, U>]
    : [First, ...Without<Rest, U>]
  : T

Solution by se030 #28316

type WithoutTuple<T, U extends number[], Result extends number[] = []> =
  T extends [infer F extends number, ...infer R extends number[]]
    ? U extends number[]
      ? F extends U[number]
        ? WithoutTuple<R, U, Result>
        : WithoutTuple<R, U, [...Result, F]>
      : never
    : Result

type Without<T, U> = U extends number
  ? WithoutTuple<T, [U]>
  : U extends number[]
    ? WithoutTuple<T, U>
    : never

Solution by jazelly #27738

type ItIsT<T, F> = F extends unknown[] ? T extends F[number] ? true : false : T extends F ? true : false
type Without<T extends unknown[], F, Result extends unknown[] = []> =
    T extends [infer Pre, ...infer Next]
      ? ItIsT<Pre, F> extends true
        ? Without<Next, F, Result>
        : Without<Next, F, [...Result, Pre]>
      : Result

Solution by jiechliu #27718

type IsExist<R, T extends any | any[]> =
  T extends [infer A, ...infer B] ? R extends A ? true : IsExist<R, B> : R extends T ? true : false


type Without<T extends any[], U, O extends any[] = []> =
  T['length'] extends 0 ? O
  :
  T extends [infer L, ...infer R]
  ?
  IsExist<L, U> extends true ? Without<R, U, O> : Without<R, U, [L, ...O]>
  : O

Solution by wuxin0011 #27329

type Without<
  T extends unknown[],
  U,
  A = Exclude<T[number], U extends unknown[] ? U[number] : U>
> = T extends [infer F, ...infer R]
  ? F extends A
    ? [F, ...Without<R, U, A>]
    : Without<R, U, A>
  : [];

Solution by DoGukKim #27301

type Without<T extends unknown[], U, Y = Exclude<T[number], U extends unknown[] ? U[number] : U>> =
  T extends [infer F, ...infer R] ? [...(F extends Y ? [F] : []), ...Without<R, U, Y>] : T

Solution by smileboyi #27105

// 你的答案
type Without<T extends unknown[], U extends number | number[]> = 
  T extends [infer X, ...infer Y] ?
    X extends (U extends number[] ? U[number] : U) ?
      Without<Y, U> : [X, ...Without<Y, U>] :
    T

Solution by kiki-zjq #25666

type _Without<T extends any[], U extends any[]> = 
  T extends [infer Head, ...infer Tail]
  ? [
    ...(Head extends U[number] ? [] : [Head]),
    ..._Without<Tail, U>
  ]
  : []
type Without<T extends any[], U> = U extends any[]
  ? _Without<T, U>
  : _Without<T, [U]>

Solution by prenaissance #24928

type Without<T extends unknown[], U extends number | number[]> = T extends [infer First, ...infer Rest] 
? First extends ( U extends number[]  
    ? U[number] 
    : U 
  )
  ? Without<Rest, U>
  : [First, ...Without<Rest, U>]
: T

Solution by NeylonR #24270

// your answers
type Without<
    T extends any[],
    U extends any[] | any,
    Res extends any[] = []
> = T extends [infer Left, ...infer Rest]
    ? Left extends Res[number]
        ? Without<Rest, U, Res>
        : Left extends (U extends any[] ? U[number] : U)
        ? Without<Rest, U, Res>
        : Without<Rest, U, [...Res, Left]>
    : Res

Solution by studymachiney #24080

type Without<T extends any[], U> = U extends any[] ?
  T extends [infer F, ...infer R] ? F extends U[number] ? [...Without<R, U>] : [F, ...Without<R, U>] : []/*pop*/ :
  Without<T, [U]>;

// old way
// type Reduce<T extends any[], U> = T extends [infer F, ...infer R] ? (U extends F ? [...Reduce<R, U>] : [F, ...Reduce<R, U>]) : [];
// type Without<T extends any[], U> = U extends any[] ?
//   (
//     U extends [infer F, ...infer R] ? Without<Reduce<T, F>, R> : T
//   ) :
//   Without<T, [U]>;

Solution by E-uler #23983

type Includes<T extends any[], U> = T extends [infer L, ...infer Rest]
    ? Equal<L, U> extends true
        ? true
        : Includes<Rest, U>
    : false;

type Without<T extends number[], U extends number | number[]> = U extends number[]
    ? T extends [infer L extends number, ...infer Rest extends number[]]
        ? Includes<U, L> extends true
            ? Without<Rest, U>
            : [L, ...Without<Rest, U>]
        : []
    : U extends number
    ? Without<T, [U]>
    : [];

Solution by asurewall #23136

type NumberToUnion<T extends unknown | unknown[]> = T extends unknown[] ? T[number] : T;
type Without<
  T, 
  U, 
  Acc extends unknown[] = []
> = T extends [infer First, ...infer Rest] 
    ? First extends NumberToUnion<U> 
      ? Without<Rest, U, Acc> 
      : Without<Rest, U, [...Acc, First]> 
    : Acc;

Solution by snakeUni #23114