05317-medium-lastindexof

Back

// 你的答案
type IsAny<T> = 0 extends T & 1 ? true : false
type LastIndexOf<T, U> = T extends [...infer A, infer R] ? 
  IsAny<U> extends true ? IsAny<R> extends true ? A['length'] : LastIndexOf<A, U> :
  [R, U] extends [U, R] ? A['length'] : LastIndexOf<A, U> : -1

Solution by heyuelan #34688

type LastIndexOf<T extends any[], U> = T extends [...infer rest, infer R] ? Equal<R, U> extends true ? rest['length'] : LastIndexOf<rest, U> : -1

Solution by ouzexi #34094

type LastIndexOf<T extends any[], U, Count extends any[] = []> = T extends [infer First, ...infer Rest]
? LastIndexOf<Rest, U, [...Count, 1]> extends -1 
    ? (<G>() => G extends First ? 1 : 2) extends (<G>() => G extends U ? 1 : 2)
      ? Count['length']
      : -1
    : LastIndexOf<Rest, U, [...Count, 1]>
  : never

Solution by PiligrimStas #33778

type SimpleEqual<U, T> = U extends T ? (T extends U ? true : false) : false;

type LastIndexOf<T extends any[], U> = 
T extends [...infer Rest, infer Item]
  ? SimpleEqual<Item, U> extends true ? Rest["length"] : LastIndexOf<Rest, U>
  : -1

Solution by sunupupup #33421

// your answers
type LastIndexOf<T extends unknown[], U>
  = T extends [...infer A, infer B]
    ? Equal<B, U> extends true
      ? A['length']
      : LastIndexOf<A, U>
    : -1

Solution by pea-sys #33113

type LastIndexOf<T extends unknown[], U>
  = T extends [...infer A, infer B]
    ? Equal<B, U> extends true
      ? A['length']
      : LastIndexOf<A, U>
    : -1

Solution by iamkanguk97 #32907

type LastIndexOf<T, U> = T extends [...infer Rest, infer F] 
  ? Equal<F, U> extends true
    ? Rest['length'] 
    : LastIndexOf<Rest, U>
  : -1

/**
Example
 LastIndexOf<[1, 2, 3, 2, 1], 2> // 3
T = [1, 2, 3, 2, 1]
U = 2
T extends [...infer Rest, infer F] 

Rest= [1, 2, 3, 2] , F = 1
As U, and F not quals ;so recursive call
LastIndexOf< [1, 2, 3, 2] , 2>
Now Rest = [1,2,3], F = 2 
Finally got the target point  Equal<F, U> extends true , because U=F  , and return Rest['length']  = 3  .

#If not exist then return -1 
 LastIndexOf<[1, 2, 3, 2, 1], 5> // -1 
Here U= 5 not existing in [1, 2, 3, 2, 1] , so function will recursively calls  and finally hit the :-1 case of the functions and return -1 .

*/

Solution by wubrafiq807 #32572

type LastIndexOf<T extends any[], U, V extends any[] = [], W extends number = -1> = T extends [infer F, ...infer L]
	? Equal<F, U> extends true
		? LastIndexOf<L, U, [...V, F], V["length"]>
		: LastIndexOf<L, U, [...V, F], W>
	: W;

Solution by gasmg #32256

type MinusOneInArray<T extends unknown[]> = 
  T extends [...infer Rest, infer _] ? Rest : never

type IsAny<T> = 0 extends (1 & T) ? true : false

type PurgeAny<T> = IsAny<T> extends true ? [never] : T

// any extends 1
type LastIndexOf<T extends unknown[], U extends unknown, Index extends number = T['length']> =
  T extends [...infer Rest, infer Last] ?
  PurgeAny<U> extends Last ?
  MinusOneInArray<T>['length'] :
  LastIndexOf<Rest, U> :
  -1

type cases = [
  Expect<Equal<LastIndexOf<[1, 2, 3, 2, 1], 2>, 3>>,
  Expect<Equal<LastIndexOf<[2, 6, 3, 8, 4, 1, 7, 3, 9], 3>, 7>>,
  Expect<Equal<LastIndexOf<[0, 0, 0], 2>, -1>>,
  Expect<Equal<LastIndexOf<[string, 2, number, 'a', number, 1], number>, 4>>,
  Expect<Equal<LastIndexOf<[string, any, 1, number, 'a', any, 1], any>, 5>>,
]

Solution by gearonixx #30938

type LastIndexOf<T extends any[], U> =
  T extends [...infer H, infer L]
    ? Equal<L, U> extends true
      ? H['length']
      : LastIndexOf<H, U>
    : -1

Solution by matallui #30813

type LastIndexOf<T, U> = T extends [...infer A, infer B] ? Equal<B, U> extends true ? A['length'] : LastIndexOf<A, U> : -1

Solution by dreamluo-plus #30664

type LastIndexOf<
  T extends unknown[],
  U,
  I extends number[] = [],
  R extends number = -1
> = T extends [infer First, ...infer Rest]
  ? LastIndexOf<
      Rest,
      U,
      [...I, 0],
      (<V>() => V extends First ? 1 : 0) extends <V>() => V extends U ? 1 : 0
        ? I["length"]
        : R
    >
  : R;

Solution by idebbarh #29863

type MyEqual<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y
  ? 1
  : 2
  ? true
  : false

type LastIndexOf<T, U extends string | number> = T extends [
  ...infer Head,
  infer Tail
]
  ? MyEqual<Tail, U> extends true
    ? Head["length"]
    : LastIndexOf<Head, U>
  : -1

Solution by Kying-star #29043

type IsEqual<A, B> = (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B
  ? 1
  : 2
  ? true
  : false;

type LastIndexOf<
  T,
  U,
  Count extends unknown[] = [],
  Res extends number = -1
> = T extends [infer F, ...infer R]
  ? IsEqual<F, U> extends true
    ? LastIndexOf<R, U, [...Count, unknown], Count["length"]>
    : LastIndexOf<R, U, [...Count, unknown], Res>
  : Res;

Solution by DoubleWoodLin #28781

type LastIndexOf<
  T extends any[],
  U extends AllTypes,
  LENGTH extends '🍎'[] = [],
  POINTER extends number = -1,
> = T extends [infer FIRST extends AllTypes, ...infer REST]
  ? `${U}` extends `${FIRST}`
    ? LastIndexOf<
        REST,
        U,
        [...LENGTH, '🍎'],
        LENGTH['length']
      >
    : LastIndexOf<REST, U, [...LENGTH, '🍎'], POINTER>
  : POINTER

Solution by playitsafe #28532

// 你的答案
type LastIndexOf<T, U, Arr extends any[] = [], C extends number = -1> = T extends [infer F, ...infer R] ? Equal<F, U> extends true ? LastIndexOf<R, U, [...Arr, 0], Arr['length']> : LastIndexOf<R, U, [...Arr, 0], C> : C

Solution by AAA611 #27898

type LastIndexOf<T extends unknown[], S, Num extends unknown[] = [], Result = -1> = T extends [] ? Result :
  Equal<T[0], S> extends true ?
    T extends [any, ...infer Next]
      ? LastIndexOf<Next, S, [...Num, 1], Num['length']>
      : T
    : T extends [any, ...infer Next]
      ? LastIndexOf<Next, S, [...Num, 1], Result>
      : Result

Solution by jiechliu #27806

type LastIndexOf<T extends any[], U extends number, M extends number = -1, N extends any[] = []> =
  T extends [infer A, ...infer B]
  ? A extends U
  ? LastIndexOf<B, U, N['length'], [...N, 0]> : LastIndexOf<B, U, M, [...N, 0]>
  : M

Solution by wuxin0011 #27335

type LastIndexOf<T extends unknown[], U> =
  T extends [...infer R, infer L] ? Equal<L, U> extends true ? R['length'] : LastIndexOf<R, U> : -1

Solution by smileboyi #27136

// 因为 1 extends number 为 true,使用 F extends U会导致测试用例通不过
// 所以使用 Equal 判定是否相等
// 而R['length'] 刚好对应了查找的下标
type LastIndexOf<T extends unknown[], U> = 
  T extends [...infer R, infer F] 
    ? Equal<F,U> extends true 
      ? R['length'] : LastIndexOf<R, U> 
    : -1

Solution by liuk123456789 #26129

// 你的答案
type MyEqual<X, Y> = 
  X extends Y 
  ? Y extends X 
    ? true
    : false
  : false 

type LastIndexOf<T extends unknown[], U extends unknown> = 
  T extends [...infer X, infer Y]
  ? MyEqual<Y, U> extends true 
    ? X['length']
    : LastIndexOf<X, U>
  : -1

Solution by kiki-zjq #25671

The idea is the same as 5153-IndexOf, but the order of the infer has changed.

// your answers
type LastIndexOf<T extends any[], U> = T extends [...infer Rest, infer L]
  ? (<K>() => K extends L ? 1 : 0) extends <K>() => K extends U ? 1 : 0
    ? Rest["length"]
    : LastIndexOf<Rest, U>
  : -1;

Solution by DvYinPo #25611

type TupleLengthMinusOne<T extends any[]> = T extends [any, ...infer Rest] ? Rest : []

type LastIndexOf<T extends any[], U, Count extends any[] = TupleLengthMinusOne<T>> =
  T extends [...infer Rest, infer R]
    ? Equal<R, U> extends true
      ? Count['length']
      : LastIndexOf<Rest, U, TupleLengthMinusOne<Count>>
    : -1

Solution by Minato1123 #25132

type LastIndexOf<T extends unknown[], U> = T extends [...infer Rest, infer Last]
? Equal<Last, U> extends true
  ? Rest['length']
  : LastIndexOf<Rest, U>
: -1

Solution by NeylonR #24365

type isEqual<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y
  ? 1
  : 2
  ? true
  : false;

type LastIndexOf<T extends any[], U> = T extends [...infer R, infer L]
  ? isEqual<L, U> extends true
    ? R['length']
    : LastIndexOf<R, U>
  : -1;

Solution by JohnLi1999 #24338

type SimpleEqual<T, U> = T extends U ? U extends T ? true : false : false;
type LastIndexOf<T extends unknown[], U, Z extends unknown[] = [], Idx extends number = -1> = T extends [infer F, ...infer R] ?
    SimpleEqual<F, U> extends true ?
    LastIndexOf<R, U, [...Z, F], Z['length']> :
    LastIndexOf<R, U, [...Z, F], Idx> :
    Idx

Solution by RBL3 #24228

// your answers
type MyEqual<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y
    ? 1
    : 2
    ? true
    : false
type LastIndexOf<
    T extends any[],
    U,
    Index extends any[] = T extends [infer Left, ...infer Rest] ? Rest : []
> = Index['length'] extends T['length']
    ? -1
    : MyEqual<T[Index['length']], U> extends true
    ? Index['length']
    : LastIndexOf<
          T,
          U,
          Index extends [infer Left, ...infer Rest] ? Rest : never
      >

Solution by studymachiney #24116

type LastIndexOf<T extends any[], U> = T extends [...infer R, infer L] ?
  Equal<L, U> extends true ? R[`length`]/*return*/ : LastIndexOf<R, U> :
  -1/*return*/;

// old way
// type SimpleEqual<T, U> = T extends U ? U extends T ? true : false : false;
// type LastIndex<T extends any[]> = T extends [any, ...infer R] ? R["length"] : -1;
// type Last<T extends any[]> = T[LastIndex<T>];

// type LastIndexOf<T extends any[], U> =
//   T extends [] ? -1 :
//   (
//     SimpleEqual<U, Last<T>> extends true ?
//     LastIndex<T> :
//     (
//       T extends [...infer R, any] ? LastIndexOf<R, U> : T
//     )
//   );

Solution by E-uler #23988


type LastIndexOf<T extends any[], E> = T extends [...infer Rest, infer L] ? Equals<L, E> extends true ? Rest['length'] : LastIndexOf<Rest, E> : -1

Solution by YangLiu1024 #23285

type LastIndexOf<T extends unknown[], U> = T extends [...infer F, infer R] ? 
Equal<U, R> extends true ? F['length'] : LastIndexOf<F, U> : -1

Solution by snakeUni #23162