Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b04d944

Browse files
author
Ram swaroop
committed
duplicates in array : comments added
1 parent 2f0c6aa commit b04d944

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/me/ramswaroop/arrays/DuplicatesInArray.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@
1212
public class DuplicatesInArray {
1313

1414
/**
15+
* Given an array of n elements which contains elements from 0 to n-1, with any of
16+
* these numbers appearing any number of times. Find these repeating numbers in O(n)
17+
* and using only constant memory space.
18+
* <p/>
19+
* For example, let n be 7 and array be {1, 2, 3, 1, 3, 6, 6}, the answer should be
20+
* 1, 3 and 6.
21+
* <p/>
22+
* EXPLANATION:
1523
* The algorithm is simple. We use index of the array to track repeating elements.
16-
* Once we encounter a element lets say 2 then we make the 2nd index -ve just to mark
17-
* that we have encountered 2. When we encounter 2 again and see that 2nd index
24+
* Once we encounter a element lets say 2 then we make the element in 2nd index -ve just
25+
* to mark that we have encountered 2. When we encounter 2 again and see that 2nd index
1826
* is already -ve we conclude that 2 is repeated.
19-
*
27+
* <p/>
2028
* Similar to {@link me.ramswaroop.arrays.TwoRepeatingElements#findTwoRepeatingElements(int[])}.
2129
*
2230
* @param a

src/me/ramswaroop/arrays/TwoRepeatingElements.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public static int[] getTwoRepeatingElements(int[] a) {
6464

6565
/**
6666
* The algorithm is simple. We use index of the array to track repeating elements.
67-
* Once we encounter a element lets say 2 then we make the 2nd index -ve just to mark
68-
* that we have encountered 2. When we encounter 2 again and see that 2nd index
67+
* Once we encounter a element lets say 2 then we make the element in 2nd index -ve just
68+
* to mark that we have encountered 2. When we encounter 2 again and see that 2nd index
6969
* is already -ve we conclude that 2 is repeated.
7070
*
7171
* Similar to {@link me.ramswaroop.arrays.DuplicatesInArray#findDuplicatesInArray(int[])}.

0 commit comments

Comments
 (0)