JSFiddle - React, Tailwind, and code Playground
JavaScript
var arr = [2, 3, 5, 8, 8, 2, 2];
var val = 2;
function s(arr, val) {
let vals = [];
let remainders = [];
arr.forEach((item, index) => {
val === item ? vals = vals.concat({item, index}) : remainders = remainders.concat(item)
});
return {
diff: vals[vals.length - 1].index - vals[0].index,
remainders
}
}
console.log(s(arr, val));