Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save kolyuchii/68969cbe983d587a7ab0b1495e3fc9d1 to your computer and use it in GitHub Desktop.

Select an option

Save kolyuchii/68969cbe983d587a7ab0b1495e3fc9d1 to your computer and use it in GitHub Desktop.
function find(arr, k) {
const obj = {};
let start = 0;
let finish = 0;
let beg = 0;
let end = 0;
for (let i = 0; i < arr.length; i += 1) {
const item = beg = end = arr[i];
if (item in obj) {
continue;
}
obj[item] = 'here';
if (item - 1 in obj) {
beg = obj[item - 1];
}
if (item + 1 in obj) {
end = obj[item + 1];
}
if (end - beg > finish - start) {
[finish, start] = [end, beg];
}
obj[end] = beg;
obj[beg] = end;
}
return [start, finish];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment