Skip to content

Commit 6beedc2

Browse files
committed
Duplicates in array
1 parent fa458e6 commit 6beedc2

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

arrays/DuplicatesInArray.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#Given an array of length n contains elements from 0 to n-1,
2+
#Print all the duplicate elements in O(n) time and constant space.
3+
4+
def print_duplicates(a)
5+
n=a.length
6+
print "Duplicate elements are: "
7+
for i in 0...n
8+
if a[a[i].abs]<0
9+
print "#{a[i].abs} "
10+
else
11+
a[a[i].abs]= -a[a[i].abs]
12+
end
13+
end
14+
return
15+
end
16+
print_duplicates([1, 4, 6, 4, 3, 6, 1]) # => Duplicate elements are: 4 6 1
17+
18+

0 commit comments

Comments
 (0)