Skip to content

@technobuddha > library > Array > Analysis

Function: longestCommonSubsequence()

ts
function longestCommonSubsequence<T>(
   array1: ArrayLike<T>, 
   array2: ArrayLike<T>, 
   options: LongestCommonSubsequenceOptions<T>): T[];

Defined in: longest-common-subsequence.ts:46

Determine the longest possible array that is subsequence of both of given arrays.

Type Parameters

Type ParameterDescription
TType of objects in the arrays.

Parameters

ParameterTypeDescription
array1ArrayLike<T>First array of objects.
array2ArrayLike<T>Second array of objects.
optionsLongestCommonSubsequenceOptions<T>Functions to compare and collect elements from the two arrays

Returns

T[]

A list of objects that are common to both arrays such that there is no common subsequence with size greater than the length of the list.

Remarks

Implementation of Longest Common Subsequence algorithm.

Example

typescript
longestCommonSubsequence(
 ['a', 'b', 'c', ' ', 'd', 'e', 'f'],
 ['a', 'c', ' ', 'd', 'e', 'c'],
); // ['a', 'c', ' ', 'd', 'e']