Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class CheckDuplicates {
- public static void main(String[] args) {
- int[] values = { 8, 5, 9, 8, 6, 13, 33, 1, 98, 12, 8, 1 };
- System.out.println(Arrays.toString(findDuplicates(values)));
- }
- public static int[] findDuplicates(int[] values) {
- int buffSize = 10;
- int[] duplicates = new int[buffSize];
- Arrays.sort(values);
- for (int i = 1, j = 0; i < values.length; i++) {
- if (values[i] == values[i - 1]) {
- duplicates[j++] = values[i];
- while (values[i] == values[i - 1]) {
- i++;
- }
- }
- if (j == buffSize - 1) {
- duplicates = growArray(duplicates, 2);
- buffSize = duplicates.length;
- }
- }
- return clipArray(duplicates);
- }
- public static int[] growArray(int[] arr, float factor) {
- int newLength = (int)(arr.length * factor);
- int[] temp = new int[newLength];
- System.arraycopy(arr, 0, temp, 0, arr.length);
- return temp;
- }
- public static int[] clipArray(int[] arr) {
- int i = 0;
- while (i < arr.length) {
- if (arr[i] == 0) {
- break;
- } else {
- i++;
- }
- }
- return Arrays.copyOfRange(arr, 0, i);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment