I am trying to print duplicate elements in one d array using for each
loop. But my output was an unexpected one. Could anyone please assist?
package Login;
public class DupsArray {
static int[] a = {1,2,3,3};
public static void main(String[] args) {
int length = a.length;
for(int i=0;i<=length-1;i++) {
for(int j : a) {
for(j=1;j<=length-1;j++) {
if(a[i]==(a[j]) ) {
System.out.println("Found duplicate");
} else {
System.out.println("No duplicates found");
}
}
}
}
}
The results show as follows:
The expected results to be print duplicate found.
4 Answers 4
Try using the below logic which compares every element with all other element in the array, if any duplicate is found,it stops the execution to continue futher
for(int i = 0; i < a.length;i++) {
for (int j = i + 1 ; j < a.length; j++) {
if (a[i] == a[j]) {
System.out.println("Found duplicate");
return;
}
}
}
System.out.println("No duplicate Found");
-
This will only print the first duplicate itemButiri Dan– Butiri Dan2019年06月03日 07:24:50 +00:00Commented Jun 3, 2019 at 7:24
-
yes. But in case we need to find all the duplicate rather than returning we can do our desired operation there.Gowtham– Gowtham2019年06月03日 07:32:09 +00:00Commented Jun 3, 2019 at 7:32
-
Traversing the array 2 timesis bad if we need to find duplicates in big arrays. This will take a lot of time to complete for bigger arrays.Thanos M– Thanos M2019年06月03日 11:33:55 +00:00Commented Jun 3, 2019 at 11:33
We can do something like this
Integer[] arr = {1, 2, 3, 3, 5, 5, 7, 8, 7};
Set<Integer> set = new HashSet<Integer>();
for (Integer i : arr) {
if (set.add(i) == false)
{
System.out.println(i);
}
}
-
yes true, but I got confused why you used 2 different hash sets.Shashank Gupta– Shashank Gupta2019年06月03日 07:48:47 +00:00Commented Jun 3, 2019 at 7:48
-
In the second set i keep the duplicates for later usage(if needed)Thanos M– Thanos M2019年06月03日 07:49:42 +00:00Commented Jun 3, 2019 at 7:49
try this and update as per your requirement
public class Main{
public static void main(String[] args) {
int[] ar = new int[] {1, 1, 3, 3, 4, 5, 7, 8};
int size = ar.length;
int error = 0;
for(int i = 0; i < size; i++){
for(int j = i+1; j < size; j++){
if(ar[i] == ar[j]){
if(i != j){
error = error + 1;
System.out.println("dulicate element " + j);
}
}
}
}
System.out.println("total number of duplicate element " + error);
}
}
You can use Sets like that :
Integer[] a = {1, 2, 3, 3, 5, 5, 7, 8, 7};
Set<Integer> duplicatesSet = new HashSet<>();
Set<Integer> helperSet = new HashSet<>();
for (Integer element : a) {
if (!helperSet.add(element)) { // NOTE*
System.out.println("Duplicate found : " + element);
duplicatesSet.add(element);
}
}
Then you can do whatever you like with the duplicates set
for(Integer el : duplicatesSet){
System.out.println(el);
}
Note*
According to javadocs :
boolean add(E e);
Adds the specified element to this set if it is not already present
return true if this set did not already contain the specified element
This way we can know if the element is already in the set, if it is we can add it to our duplicates.