Function findDuplicates

  • Identifies duplicate elements in an array and returns them as a new array.

    Returns

    An array containing all elements that appear more than once in the input array. Each duplicate element appears only once in the returned array, regardless of how many times it occurs.

    Example

    findDuplicates([1, 2, 3, 2, 4, 3, 5]); // → [2, 3]
    findDuplicates(['a', 'b', 'a', 'c']); // → ['a']

    Remarks

    • Uses Set internally for efficient duplicate detection.
    • Preserves the insertion order of first detected duplicates.

    Type Parameters

    • T

      The type of elements in the array.

    Parameters

    • arr: T[]

      The array to check for duplicate values.

    Returns T[]

Generated using TypeDoc