const getLongest = seq => seq.reduce(({
longest,
currentItem,
currentLength,
items
}, item) => {
const newCurrentLength = currentItem === item ? currentLength + 1 : 1;
if (newCurrentLength > longest) {
// we have a new longest sequence, assign new values
// to longest and items
return {
currentItem: item,
currentLength: newCurrentLength,
longest: newCurrentLength,
items: 1,
};
} else if (newCurrentLength === longest) {
// we match the previously longest,
// add the item to the sequence list
return {
currentItem: item,
currentLength: longest,
longest,
items: items + 1,
};
} else {
// this is still a shorter sequence,
// just update the local state
return {
currentItem: item,
currentLength: newCurrentLength,
longest,
items
}
}
return ;
}, {
longest: 0,
currentItem: undefined,
currentLength: 0,
items: 0
});
const values = ["11ADD", "115222", "ABCDEF10"];
const res = values.map(s => s.split("")).map(getLongest);
console.log(res);