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

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

GeeksforGeeks

8 лет назад

138,742 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

GAURAV GHOSH
GAURAV GHOSH - 27.09.2023 21:33

great solution. I had a doubt:what if an element is repeated 4 times then it's index will be negative and we will insert that index twice in our ans.

Ответить
Tushar Singh
Tushar Singh - 13.11.2022 17:29

Thank You So Much ..................🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

Ответить
Sang Nguyenn
Sang Nguyenn - 08.09.2022 14:28

😍😍😍😍😍😍😍😍

Ответить
Sang Nguyenn
Sang Nguyenn - 08.09.2022 14:18

Thank you so much

Ответить
Prasanna PM
Prasanna PM - 02.07.2022 13:30

thanks!

Ответить
Sheenamm mairaj785
Sheenamm mairaj785 - 14.07.2021 10:48

Plz add english subtitles in ur videos..cz sometimes voice not clear this will help me a lot If you add English subtitle in your video

Ответить
Kaif Ahmad
Kaif Ahmad - 05.06.2021 16:46

Yet another great problem Explained fabulously.

Ответить
We Can
We Can - 27.02.2021 11:42

vector<int> duplicates(int arr[], int n)
{
map<int,int>mp;
vector<int>v;
int c = 1;
for(int i = 0; i < n; i++)
{
int ind = arr[i] % n;
arr[ind] += n;

if(arr[ind]/n == 2)
{
v.push_back(ind);
c = 0;

}
}
sort(v.begin(),v.end());

if(c)
{
v.push_back(-1);
return v;
}
else
{
return v;
}
}

Ответить
Yasser Hussain
Yasser Hussain - 12.02.2021 08:40

The solution is good but the way you explain is so bad. Might as well just post the solution and get it over with.

Edit:- Just checked its a wrong solution. Doesn't work for
int arr[] = {2, 2, 0}

Ответить
Sharan Pai
Sharan Pai - 22.01.2021 17:18

What if a[i] is greater than length of the array

Ответить
Gaurav Sonkusle
Gaurav Sonkusle - 15.01.2021 23:21

Better approach in the article:- First a[ a[i]%n ] = a[ a[i]%n ] + n; then, a[i] is repeated element if a[i]%n >= 2; n is len(a)

Ответить
Meghana Vaitla
Meghana Vaitla - 26.09.2020 17:53

what if we have -1 and 0 if we increment all elements by 1 then -1 becomes 0

Ответить
Pragyan anand
Pragyan anand - 03.09.2020 00:22

Buffer overflow / segfault may occur.

Ответить
Baba
Baba - 19.08.2020 18:10

Geeks for geeks solution vjdeo sucks...

Ответить
Bipin
Bipin - 09.08.2020 08:48

This won't work all cases, the efficient I could write with the time of O(n) => one-time traverse through the array
```
def find_duplicates(nums)
tracker = {}
result = []
nums.each do |n|
if tracker[n]
result << n
else
tracker[n] = true
end
end
result
end
```

Ответить
Neeraj kumar
Neeraj kumar - 04.08.2020 11:03

wont work if array is constant

Ответить
Aayush
Aayush - 12.07.2020 16:32

What if the array is read only?

Ответить
saurabh
saurabh - 14.06.2020 15:24

can we don something like this if numbers beyond n - 1 present in array

Ответить
Kaleshwar Hallikerimath
Kaleshwar Hallikerimath - 10.06.2020 01:01

I think Nick White had a video on this.

Ответить
Hermes Mercurius Trismegistus
Hermes Mercurius Trismegistus - 29.04.2020 18:54

Amazing! Nice neat efficient solution but how would someone come on fly with such a solution in an interview especially under stress???!! Coming with such a solution is a marathon!

Ответить
imran zafar
imran zafar - 16.02.2020 17:32

create a map of occurrence of the elements in the array and then if their occurrence value is greater than one push them in the resultant array. time complexity O(n)

Ответить
shirshak
shirshak - 13.11.2019 19:41

this is not algorithm but shit. It wont work for 0. It wont work for anything that is greater than size of array. And the title is Find duplicates in O(n) time and O(1) extra space. I think I should never go to geeksforgeeks. At least they should validate the algorithm on real case situation.

Ответить
Dexter Aparicio
Dexter Aparicio - 12.10.2019 21:55

Given arr = {1,2,3}; is an invalid input because it contains the element 3. You said, 0 to n-1 only therefore the value of 3 is illegal. You cannot also allow element of 0 because you cannot negate it. So this algo assumes that at least there is a repeating element to satisfy the constraint. For example, arr = {1,2,1}; is valid input. So many constraint, and a-priori requirements. But the curious funny part is that the pre-condition that at least one element must repeat to make the input valid is also the problem that the algo is meant to determine.

Ответить
Bharti Chhatwani
Bharti Chhatwani - 03.10.2019 18:16

There should be a pre condition : all element's value is less than n-1

Ответить
himanshu uniyal
himanshu uniyal - 23.09.2019 11:28

wrong

Ответить
L B
L B - 26.07.2019 05:50

thanks pajeet!

Ответить
Arsalan Khan
Arsalan Khan - 14.07.2019 12:40

plz hlep

Ответить
Arsalan Khan
Arsalan Khan - 14.07.2019 12:39

an alien needs to enter the password to board the spaceship can you help him recall the 4 digits code with non-repeated digits using these clues.
1.The third digit is the smallest prime number.
2.The fourth digit is the product of the secound and third numbers.
3.The first digit is the difference between the fourth and third digits.
4.only one the digit is an odd number.
5. None of the digits are greater than 8.

Ответить
Saurav Suman
Saurav Suman - 13.07.2019 07:49

This algo is failing for a simple scenario where, there is no duplicates, it still shows repeating element, like if we call the method on array {1,2,3} it will print 3 as a repeating element while its not.

Ответить
tushar upadhyay
tushar upadhyay - 31.05.2019 14:00

What if all elements in array are greater than its index value

Ответить
avinash pathak
avinash pathak - 21.04.2019 22:28

what will happen when [10,10,10]

Ответить
Synergy12 session
Synergy12 session - 10.04.2019 22:48

wrong solution

Ответить
Naman Aggarwal
Naman Aggarwal - 20.03.2019 21:50

This approach will not work if duplicates' frequency >=3 .. In that case same elements will be printed twice or as many times as their (frequency-1)
Thus we wont be able to print uniquely duplicate elements

Ответить
Hassan Essam
Hassan Essam - 12.03.2019 02:08

MEMORY VIOLATION
check if the array as following Arr = [2^31, 1, 2, 1]
it needs to access Arr[2^31] !!
which is not allowed if the memory protection unit is enabled in the compiler

Ответить
Xinwei Xiong
Xinwei Xiong - 11.02.2019 18:15

The first note of that question: You must not modify the array (assume the array is read only).

Ответить
Rafay Khan
Rafay Khan - 01.02.2019 18:23

very nice! Regurgitated the slides without explaining the algo,

Ответить
Haresh Karnan
Haresh Karnan - 12.01.2019 22:07

It should be A[abs(A[i])-1] and not A[abs(A[i])] . Array out of bounds error !. But great explanation. Thanks !

Ответить
Sudheer acharya
Sudheer acharya - 12.12.2018 22:11

its not working number 1,2,3,1,6,6,49,49...dumb ass code

Ответить
rodrigo insignares
rodrigo insignares - 31.10.2018 03:48

Given an array (Dimension 6), check if array doesn’t have duplicates, and how many duplicates are there
plsssssssssssssss

Ответить
Pratyush Pare
Pratyush Pare - 02.09.2018 13:49

Apart from this question is there any solution which can handle duplicacy of both +ive and -ive number with same complexity and if not tnen what will be the best suitable method for this problem ?

Ответить
Mohammed Julfikar Ali Mahbub
Mohammed Julfikar Ali Mahbub - 01.09.2018 10:40

Apart from various issues mentioned in the comment among which most of the issues are valid but my question is how will you handle an array having both -1 and 0 repeated as increment by +1 will make -1 as 0 ...

Ответить
Ankit Raj
Ankit Raj - 28.07.2018 14:14

what if arr[]={1,2,3,1,3,3,1,6,6} ??
Actually this code will print the duplicates no. more than 1,which should not be the output.

Ответить
Shaun Sawyer
Shaun Sawyer - 13.06.2018 16:41

Should be changed from 0..n-1 to 1..n-1 as I don't think 0 will work. If 0 occurs at index i and the number i is duplicated in the array we won't be able to mark it as negative.

Ответить
Fahad Mudassar
Fahad Mudassar - 26.05.2018 08:45

This code is wrong. It has dead lock in it.

Ответить
Chandan Gurjar
Chandan Gurjar - 11.03.2018 21:54

Is modifying the content of the array not equal to using extra Space?
Think about it. The method calling the RemoveDuplicates method can no longer use the array as it is corrupted.. So in the real world, it amounts to doing it with O(n) space as the calling method has to create a duplicate array and pass it on..., even though not obvious at first.

Ответить
Chandan Gurjar
Chandan Gurjar - 11.03.2018 21:47

Wondering where in real world , we need to solve this problem with this constraint?
The constraint of the input to contain only elements from 0 to n-1, where n is the length of the array is not what you would encounter in real world

Ответить
Tincu Stefan Lucian
Tincu Stefan Lucian - 22.02.2018 23:59

The title is misleading because it's a generic affirmation which is expected to work in all cases but it's not. It's like saying pigs can fly! Yeah only pigs in planes fly or with some device attached because the plane flies or the other device flies.

Back to the problem: It works only for some very specific arrays which don't have elements bigger than the size of array and no negative numbers and the array is corrupted at the end so finally it's O(n) space. Also if the array starts with 0, 0, .... it cannot detect double 0.

Ответить
Vasu Arora
Vasu Arora - 15.02.2018 15:44

If an element is repeating n times, this solution will print it n-1 times.

Ответить
Nikki S
Nikki S - 12.12.2017 01:16

Super clever! Thank you!

Ответить
KonstantinGeist
KonstantinGeist - 22.11.2017 20:30

It wasn't stated input parameters can be modified. Bad solution in 2017 when we want to make everything we can const for better parallelization. This is probably something from the 1970's.

Ответить