This is most common interview question in java now-a-days. There are many techniques to find duplicate elements in array in java like using Collections.frequency(). I am writing yet another solution which is much easier and fast.
Here an array of integers is having 10 integers and 1 and 8 are duplicate integers. You need to filter them out.
package com.howtodoinjava.interview; import java.util.HashSet; import java.util.Set; public class DuplicateInArray { public static void main(String[] args) { int[] array = {1,1,2,3,4,5,6,7,8,8}; Set<Integer> set = new HashSet<Integer>(); for(int i = 0; i < array.length ; i++) { //If same integer is already present then add method will return FALSE if(set.add(array[i]) == false) { System.out.println("Duplicate element found : " + array[i]); } } } } Output: Duplicate element found : 1 Duplicate element found : 8
In this solution, all you have to do is to iterate over array and all elements in a set one by one. If Set.add() method returns false then element is already present in set and thus it is duplicate.
Happy Learning !!
Was this post helpful?
Let us know if you liked the post. That’s the only way we can improve.
also count the duplicate no. occurances?
display
The type Set is not generic; it cannot be parameterized with arguments
In interview i have faced one question.
How to find second repeated value in Array?
Ex: {1,2,3,4,7,8,4,6,9,5,8,0,1}
o/p: 8
What is the solution?
public static void main(String []args){
int[] array={2,3,5,2,5,6,9,6,4,7};
int count=0;
int newcount;
int[] duparray=new int[count];
for(int i=0;i<array.length;i++){
for(int j=i+1;j<array.length;j++){
if(array[i]==array[j]){
count++;
if(count==2){
System.out.println("The Second repeated value in an array is" +array[i]);
}
}
}
}
}
Ignore duparray and newcount
Great analysis. I would recommend another post about the same question – http://blog.gainlo.co/index.php/2016/05/10/duplicate-elements-of-an-array/?utm_source=comment&utm_campaign=comment&utm_medium=11316
Hi I need
program for Duplicate array store in another array?
Hi,
Is this optimized one ? time complexity O(n)
public static void main(String[] args) {
// TODO Auto-generated method stub
int A[] = {4,2,6,5,3,8,6};
List ar = Arrays.asList(IntStream.of(A).boxed().toArray());
int dist[] = Arrays.stream(A).distinct().toArray();
int count;
int maxCount =0;
int leader = 0;
for(int k : dist){
count = Collections.frequency(ar, k);
if(count > maxCount ){
leader = k;
maxCount = count;
}
}
System.out.println(“Leader of array:”+leader);
}
What if I want to determine the number of duplicates, the total of duplicates?
To avoid printing many times, another solution for taking the strategy space for time:
int[] array={10,10,20,10,20,41,55,10,10,10,11,12,66};
Set set = new HashSet();
Set set2 = new HashSet();
for(int i=0; i<array.length; i++){
if(!set.add(array[i])){
if(set2.add(array[i]))
System.out.println(array[i]);
}
}
Thanks All 🙂
Hello,
int[] arr={10,10,20,10,20,41,55,10,10,10,11,12,66};
Your program may fail for this array declaration it will print 5 times 10 s
so make it correct as below:
int[] arr={10,10,20,10,20,41,55,10,10,10,11,12,66};
Set set=new HashSet();
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{
if(arr[i]==arr[j])
{
if(!set.contains(arr[i]))
{
set.add(arr[i]);
System.out.println("Duplicate Element:"+arr[i]);
}
}
}//end inner for;
}//end outer for
Very clever. Thanks for sharing with others. Much appreciated.
Super bro
@Lokesh perfect one just to add another similiar approach for this is ..
public class dupliacteselection {
public static void main(String[] args) {
int a[]={1,2,3,3,4};
Set s=new HashSet();
for(int i=0;i<a.length;i++)
{
if(!s.add(a[i]))
System.out.println("at index"+ i+" "+a[i]+"is duplicate");
}
for(int i:s)
{
System.out.println(i);
}
}
}
import java.util.HashSet;
import java.util.Set;
public class DuplicatItems {
public static void main(String[] args) {
int items[] = {1,2,3,2,5,7,6,4,5,5,9};
Set set = new HashSet();
for (int item:items){
if(set.contains(item))
System.out.println(item +” is duplicate item”);
set.add(item);
}
System.out.println(“No. of duplicate items: “+(items.length-set.size()));
}
}
Thanks Vivek for sharing your answer. It’s almost same, with additional call to
set.contains()
method.Hi Lokesh,
How to find the total number of times a value is repeated in a HashMap containing duplicate values ?
May be you can use Map, key as element in array and frequency as value.
Something like
if(map.contains(key)){
map.get(key)+1;
}else{
map.put(key, new Integer(1));
}
Thanks Lokesh 🙂 this works for me
Thanks for this.
they asked me this recently