// This program has been developed by students from the bachelor Computer Science at Utrecht
// University within the Software Project course.
// © Copyright Utrecht University (Department of Information and Computing Sciences)
/**
 * Find the indices of all elements that occur more than once.
 *
 * @param array The array to search for duplicates.
 * @returns An array of indices where an element occurs more than once, in no particular order.
 */
export default function duplicateIndices<T>(array: T[]): number[] {
  const positions = new Map<T, number[]>();

  array.forEach((value, i) => {
    if (!positions.has(value)) positions.set(value, []);
    positions.get(value)!.push(i);
  });

  // flatten all index lists with more than one element
  return Array.from(positions.values())
    .filter(idxs => idxs.length > 1)
    .flat();
}
