Determine indices of adjacent duplicate array elements

Solution for Determine indices of adjacent duplicate array elements
is Given Below:

Hey I am new to asking questions here so I hope I do it right. I am trying to figure out how to take an array of numbers (unsorted as I need their original index) and find any elements that are same, but the only ones I care about are the elements in the array that are the same, that are next to each other. In all instances, this will only happen one time. So any other duplicates, regardless of their value, they are meaningless. I only need the duplicates next to each other in the array.

The second part of my question is once I have located the duplicates in the array that are next to each other, I need to find out the index of that pair.

function yArrayDiff(yLandingZone, n) {
    let toFindDuplicates = totalY => totalY.filter((item, index) => totalY.indexOf(item) !== index)
    yLandingZone = toFindDuplicates(totalY);
    // if ()
    return yLandingZone;
};

This gives me my duplicates, but I am unable to find a way to modify it to do what I need it to.

Generator solution:

let getAdjacentDupIndices = function*(arr) {
  // Yields every index `n` in `arr` such that either arr[n] === arr[n + 1],
  // or arr[n] === arr[n - 1]
  for (let i = 1; i < arr.length; i++) if (arr[i] === arr[i - 1]) { yield i - 1; yield i; }
};

In action:

let getAdjacentDupIndices = function*(arr) {
  for (let i = 1; i < arr.length; i++) if (arr[i] === arr[i - 1]) { yield i - 1; yield i; }
};

let tests = [
  [ 0 ],
  [ 0, 0 ],
  [ 0, 1 ],
  [ 0, 1, 2, 3, 4, 5, 6, 7 ],
  [ 0, 1, 2, 2, 3, 4, 5, 6 ],
  [ 0, 0, 1, 2, 2, 3, 4, 5 ],
  [ 0, 0, 1, 1, 2, 2, 3, 3 ],
  [ 1000, 1500, 1400, 2100, 2100, 200, 500, 300, 200, 1000, 500, 800, 200, 800, 600, 1200, 900, 500, 300, 500 ]
];

for (let arr of tests) {
  console.log(`Adjacent inds for [ ${arr.join(', ')} ]:`);
  
  let has = false;
  for (let ind of getAdjacentDupIndices(arr)) {
    has = true;
    console.log(`  ${ind}`);
  }
  if (!has) console.log(`  <none>`);
}

And don’t forget that it’s very easy to get an Array of results from a generator; you just need to do:

let resultsArr = [ ...getAdjacentDupIndices([ 1, 1, 2, 2, 3, 3 ]) ];

If I understand correctly, here is an example of collecting indexes of adjacent duplicates and using this collection to change the corresponding elements:

const array = [0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1];
console.log(array.join(' '));

const dupIndexes = new Set();

for (let i = 0; i < array.length - 1; i++) {
  if (array[i] === array[i + 1]) dupIndexes.add(i).add(i + 1);
}

console.log(...dupIndexes);

for (const i of dupIndexes) {
  array[i] = '-';
}

console.log(array.join(' '));

Determine indices of adjacent duplicate array elements

Solution for Determine indices of adjacent duplicate array elements
is Given Below:

Hey I am new to asking questions here so I hope I do it right. I am trying to figure out how to take an array of numbers (unsorted as I need their original index) and find any elements that are same, but the only ones I care about are the elements in the array that are the same, that are next to each other. In all instances, this will only happen one time. So any other duplicates, regardless of their value, they are meaningless. I only need the duplicates next to each other in the array.

The second part of my question is once I have located the duplicates in the array that are next to each other, I need to find out the index of that pair.

function yArrayDiff(yLandingZone, n) {
    let toFindDuplicates = totalY => totalY.filter((item, index) => totalY.indexOf(item) !== index)
    yLandingZone = toFindDuplicates(totalY);
    // if ()
    return yLandingZone;
};

This gives me my duplicates, but I am unable to find a way to modify it to do what I need it to.

Generator solution:

let getAdjacentDupIndices = function*(arr) {
  // Yields every index `n` in `arr` such that either arr[n] === arr[n + 1],
  // or arr[n] === arr[n - 1]
  for (let i = 1; i < arr.length; i++) if (arr[i] === arr[i - 1]) { yield i - 1; yield i; }
};

In action:

let getAdjacentDupIndices = function*(arr) {
  for (let i = 1; i < arr.length; i++) if (arr[i] === arr[i - 1]) { yield i - 1; yield i; }
};

let tests = [
  [ 0 ],
  [ 0, 0 ],
  [ 0, 1 ],
  [ 0, 1, 2, 3, 4, 5, 6, 7 ],
  [ 0, 1, 2, 2, 3, 4, 5, 6 ],
  [ 0, 0, 1, 2, 2, 3, 4, 5 ],
  [ 0, 0, 1, 1, 2, 2, 3, 3 ],
  [ 1000, 1500, 1400, 2100, 2100, 200, 500, 300, 200, 1000, 500, 800, 200, 800, 600, 1200, 900, 500, 300, 500 ]
];

for (let arr of tests) {
  console.log(`Adjacent inds for [ ${arr.join(', ')} ]:`);
  
  let has = false;
  for (let ind of getAdjacentDupIndices(arr)) {
    has = true;
    console.log(`  ${ind}`);
  }
  if (!has) console.log(`  <none>`);
}

And don’t forget that it’s very easy to get an Array of results from a generator; you just need to do:

let resultsArr = [ ...getAdjacentDupIndices([ 1, 1, 2, 2, 3, 3 ]) ];

If I understand correctly, here is an example of collecting indexes of adjacent duplicates and using this collection to change the corresponding elements:

const array = [0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1];
console.log(array.join(' '));

const dupIndexes = new Set();

for (let i = 0; i < array.length - 1; i++) {
  if (array[i] === array[i + 1]) dupIndexes.add(i).add(i + 1);
}

console.log(...dupIndexes);

for (const i of dupIndexes) {
  array[i] = '-';
}

console.log(array.join(' '));