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

Commit 2f0c6aa

Browse files
author
Ram swaroop
committed
duplicates in array : done
1 parent 723beec commit 2f0c6aa

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package me.ramswaroop.arrays;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by IntelliJ IDEA.
7+
*
8+
* @author: ramswaroop
9+
* @date: 8/21/15
10+
* @time: 9:41 AM
11+
*/
12+
public class DuplicatesInArray {
13+
14+
/**
15+
* 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
18+
* is already -ve we conclude that 2 is repeated.
19+
*
20+
* Similar to {@link me.ramswaroop.arrays.TwoRepeatingElements#findTwoRepeatingElements(int[])}.
21+
*
22+
* @param a
23+
* @return
24+
*/
25+
public static int[] findDuplicatesInArray(int[] a) {
26+
int[] duplicates = new int[a.length];
27+
28+
for (int i = 0, j = 0; i < a.length; i++) {
29+
if (a[Math.abs(a[i])] >= 0) {
30+
a[Math.abs(a[i])] = -a[Math.abs(a[i])];
31+
} else {
32+
duplicates[j++] = Math.abs(a[i]);
33+
}
34+
}
35+
return duplicates;
36+
}
37+
38+
public static void main(String a[]) {
39+
System.out.println(Arrays.toString(findDuplicatesInArray(new int[]{1, 1, 2, 3, 1, 3, 6, 6})));
40+
// doesn't work if 0 is present in array (as -0 makes no sense but we can modify the algorithm to handle 0)
41+
System.out.println(Arrays.toString(findDuplicatesInArray(new int[]{1, 0, 1, 2, 3, 1, 3, 6, 6})));
42+
System.out.println(Arrays.toString(findDuplicatesInArray(new int[]{0, 0, 1, 2, 3, 1, 3, 6, 6})));
43+
}
44+
}

src/me/ramswaroop/arrays/TwoRepeatingElements.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,37 @@ public static int[] getTwoRepeatingElements(int[] a) {
6262
return new int[]{x, y};
6363
}
6464

65+
/**
66+
* 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
69+
* is already -ve we conclude that 2 is repeated.
70+
*
71+
* Similar to {@link me.ramswaroop.arrays.DuplicatesInArray#findDuplicatesInArray(int[])}.
72+
*
73+
* @param a
74+
* @return
75+
*/
76+
public static int[] findTwoRepeatingElements(int[] a) {
77+
int[] repeatingElements = new int[2];
78+
79+
for (int i = 0, j = 0; i < a.length; i++) {
80+
if (a[Math.abs(a[i])] >= 0) {
81+
a[Math.abs(a[i])] = -a[Math.abs(a[i])];
82+
} else {
83+
repeatingElements[j++] = Math.abs(a[i]);
84+
}
85+
}
86+
return repeatingElements;
87+
}
88+
6589
public static void main(String a[]) {
6690
System.out.println(Arrays.toString(getTwoRepeatingElements(new int[]{4, 2, 4, 5, 2, 3, 1})));
6791
System.out.println(Arrays.toString(getTwoRepeatingElements(new int[]{2, 4, 5, 2, 3, 1, 6, 7, 7})));
6892
System.out.println(Arrays.toString(getTwoRepeatingElements(new int[]{1, 2, 1, 2})));
93+
System.out.println("========");
94+
System.out.println(Arrays.toString(findTwoRepeatingElements(new int[]{4, 2, 4, 5, 2, 3, 1})));
95+
System.out.println(Arrays.toString(findTwoRepeatingElements(new int[]{2, 4, 5, 2, 3, 1, 6, 7, 7})));
96+
System.out.println(Arrays.toString(findTwoRepeatingElements(new int[]{1, 2, 1, 2})));
6997
}
7098
}

0 commit comments

Comments
 (0)