Find duplicates in O(n) time and O(1) extra space

preview_player
Показать описание
This is gfg question that has been asked in faang interview:-

Given an array of n elements that contains elements from 0 to n-1, with any of these numbers appearing any number of times.
Find these repeating numbers in O(n) and use only constant memory space.
Note: The repeating element should be printed only once.
Example:
Input: n=7 , array[]={1, 2, 3, 6, 3, 6, 1}
Output: 1, 3, 6
Explanation: The numbers 1 , 3 and 6 appears more than once in the array.
Input : n = 5 and array[] = {1, 2, 3, 4 ,3}
Output: 3
Explanation: The number 3 appears more than once in the array.

similar question on leetcode:
Рекомендации по теме