Simple code to see if array of primitives has duplicates. The aim is to only check if there are any duplicates, but not to check what element is repeated, or its position etc.
PS: Do you know some handy snippet? Don't way, post it!
Simple code to see if array of primitives has duplicates. The aim is to only check if there are any duplicates, but not to check what element is repeated, or its position etc.
PS: Do you know some handy snippet? Don't way, post it!
import java.util.*;
public class QuickCoding {
public static void main(String[] args){
int myArray[] = {0, 1, 0, 0, 2, 0, 0, 3, 0, 1};
List<Integer> list = new ArrayList(Arrays.asList(myArray));
Set<Integer> set = new HashSet<Integer>(list);
if(set.size() < myArray.length){
System.out.println("There are some duplicates");
}
}
}
import java.util.*;
public class QuickCoding {
public static void main(String[] args){
int myArray[] = {1, 2, 3,5,1};
List list = new ArrayList();
for(int i=0;i<myArray.length;i++)
list.add(myArray[i]);
Set<Integer> set = new HashSet<Integer>(list);
if(set.size() < myArray.length){
System.out.println("There are some duplicates");
}
}
}
@Muralidharan.E sorry, but your code is just de-tour from code posted by me. As you can see from above your for loop is not need it, if you use appropriate class to convert from collection (array) to collection(integers list) loop is obsolete.
Set<Integer> set = new HashSet<Integer>(list);
Always set size is showing as 1. so i used loop without thinking. Now i will try to workout without loop.
Thank you Peter!
The original code snippet is quite good. Hard to "top" it.
If there a very large number of values and duplicates were expected to be common, and performance was a real concern, I might loop and exit early:
int myArray[] = {0, 1, 0, 0, 2, 0, 0, 3, 0, 1};
Set<Integer> dups = new HashSet<Integer>();
for (Integer value : myArray) {
if (dups.contains(value)) {
System.out.println("There are some duplicates");
break;
} else {
dups.add(value);
}
}
But I can't say that this code is "generally better" than the original code.
You can skip the intermediate list creation, by using Collections.addAll with varargs, if the array is of an object type or just a variable number of method arguments (varargs). It does not work for arrays of primitives (like int), though:
package net.doepner;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class ArraysUtil {
public static <T> boolean containsDuplicates(T... elements) {
final Set<T> set = new HashSet<T>();
Collections.addAll(set, elements);
return set.size() < elements.length;
}
public static void main(String[] args) {
final Integer[] test = { 1, 3, 5, 1 };
System.out.println(containsDuplicates(test));
// => true
final String a = "foo";
final String b = "bar";
final String c = "bar";
System.out.println(containsDuplicates(a, b, c));
// => true
}
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.