first repeated element in an array???????
Page 1 of 1 • Share •
first repeated element in an array???????
give an algo in o(n) to find first repeated element in an array?
e.g.{1,2,3,4,2,1,5,6}
output=2
e.g.{1,2,3,4,2,1,5,6}
output=2
ish_mnnit- Posts: 4
Join date: 2008-08-10
Re: first repeated element in an array???????
giving it a try....
You can use Hash Tables.
in O(1) it will search the element present in the hash table and the moment any of it's entries list is appended for the first time you report that element as the first repeated element.
You can use Hash Tables.
in O(1) it will search the element present in the hash table and the moment any of it's entries list is appended for the first time you report that element as the first repeated element.
Beagle- Posts: 9
Join date: 2008-08-12
Re: first repeated element in an array???????
no idea about hash table
plz elaborate a li'l
plz elaborate a li'l

ankurgutpa- Posts: 44
Join date: 2008-08-10
Age: 22
Location: Tandon 72
Re: first repeated element in an array???????
yaar u can always search for hash tables on wikipedia, it's the idea that i have provided. and i think my idea is correct.
Beagle- Posts: 9
Join date: 2008-08-12
Re: first repeated element in an array???????
what about implementation of so called HASH TABLES??

ankurgutpa- Posts: 44
Join date: 2008-08-10
Age: 22
Location: Tandon 72
Re: first repeated element in an array???????
ok giving it a try...
From the domain of numbers specified, The hash table will be created(I don't exactly remember the process of creating a hash table) but it's from the domain of inputs from which it takes it's values.
Now having created your hash table, the values will be searched and inserted in O(1) time complexity.
Now the case arise that domain has repeated values(don't consider the domain of sets) so entering an existing value would create a problem(as it already exists), To overcome this problem a link is created to the value to be inserted like
1
2-2
3
4
5-5-5
and so on....
so when a link is created for the hash table for all the values,(that existed prior to the value that is being currently added) flag is raised denoting that a link has been created. The moment this is done for any value, report that to be as the first repeated element.
Correct me if wrong.....
From the domain of numbers specified, The hash table will be created(I don't exactly remember the process of creating a hash table) but it's from the domain of inputs from which it takes it's values.
Now having created your hash table, the values will be searched and inserted in O(1) time complexity.
Now the case arise that domain has repeated values(don't consider the domain of sets) so entering an existing value would create a problem(as it already exists), To overcome this problem a link is created to the value to be inserted like
1
2-2
3
4
5-5-5
and so on....
so when a link is created for the hash table for all the values,(that existed prior to the value that is being currently added) flag is raised denoting that a link has been created. The moment this is done for any value, report that to be as the first repeated element.
Correct me if wrong.....
Beagle- Posts: 9
Join date: 2008-08-12
Re: first repeated element in an array???????
Do U Know How MAP works in C++.
It must be using Some Hash Function ...
You Can Do It .. this way too..
But the point is .. Check Out If It searches in O(1) time..
It must be using Some Hash Function ...
You Can Do It .. this way too..
But the point is .. Check Out If It searches in O(1) time..

Dee2306- Posts: 7
Join date: 2008-08-12
Age: 21
Re: first repeated element in an array???????
I can't say about map... but for hash tables i m pretty much sure that it searches in O(1) time, if the element is present only once, and O(n) [n is the number of times the element is repeated] otherwise.
Beagle- Posts: 9
Join date: 2008-08-12
Permissions of this forum:
You cannot reply to topics in this forum





