this is my algorithm that I have written it with my friends (which are in stackoverflow site)
this algorithm will find just the first duplicate number and returns it.this works in O(n)
I want to complete this algorithm that helps me to get duplicate numbers with their repetition. consider that I have [1,1,3,0,5,1,5]
I want this algorithm to return 2 duplicate numbers which are 1 and 5 with their repetition which is 3 and 2 respectively .how can I do this with O(n)?
1 Algorithm Duplicate(arr[1:n],n)
2
3 {
4 Set s = new HashSet();i:=0;
5 while i<a.size() do
6 {
7 if(!s.add(a[i)) then
8 {
9 return a[i]; //this is a duplicate value!
10 break;
11 }
12 i++;
13 }
14 }
You can do this in Java:
Instead of iterating each time to get count of duplicate it’s better to store the count in map.