Find Duplicates in an array
package com.blogspot.rajvishwakarma15;
public class CheckDuplictesInArray {
public static void main(String[] args) {
String[] strArr = new String[] { "a", "b", "c", "s", "d", "e", "a",
"b", "s" };
detectDuplicates(strArr);
}
public static void detectDuplicates(String[] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] == arr[i]) {
System.out.println("Duplicate found! Original was at location "
+ (i + 1) + " and match is at " + (j + 1));
System.out.println("Duplicated value is:" + arr[j]);
}
}
}
}
}
OUTPUT
Duplicate found! Original was at location 1 and match is at 7 Duplicated value is:a
Duplicate found! Original was at location 2 and match is at 8 Duplicated value is:b
Duplicate found! Original was at location 4 and match is at 9 Duplicated value is:s
No comments:
Post a Comment