MainMenu

Home Java Overview Maven Tutorials

Thursday, 28 June 2018

Find Duplicate Element in Array, Find count of duplicate element in array, remove duplicate element from array



Hello Friends,

In this article , we will learn , Three below things :-
1). How to Find Duplicate Element in array
2). How to find count of duplicate element in array.
3). How to remove duplicate element from array.
Note : It is very important tutorial for interview purpose.

Video :


It can be achieved by simple below algorithm :-
First create a collection.
now compare each element, within this collection
If collection have that element, than ignore else add this element to collection.

Below is Sample code :-


package Chandan_Java.Chandan_Java;
import java.util.*;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class PojorunExample
{
@Test
public void g()
{
int x[] = {2, 9 , 5, 2, 3, 5, 7, 9, 3, 2};
List<Integer> list = new ArrayList<Integer>();
list.add(x[0]);
for(int i =0; i<x.length; i++)
{
if(list.contains(x[i]))
{
}
else
{
list.add(x[i]);
}
}
for(int j = 0; j<list.size(); j++)
{
System.out.println(list.get(j));
}
}
}

OUTPUT :-

2
9
5
3
7





Another Way to Remove Duplicate Elements from an Array Using HashSet Class of Set Interface

public class PojorunExample
{
@Test
public void g()
{
int x[] = {2, 9 , 5, 2, 3, 5, 7, 9, 3, 2};
Set<Integer> hs = new HashSet<Integer>();
for(int i =0; i<x.length; i++)
{
hs.add(x[i]);
}
for(int z : hs)
{
System.out.println(z);
}
}
}
OUTPUT :-
2
9
5
3
7


Example to get Duplicate Emailement from an Array & Occurance of duplicate element using HashMap class of Map Interface

package Chandan_Java.Chandan_Java;

import java.util.*;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class JavaInterview
{
@Test
public void g()
{
int x[] = {2, 2, 9 , 5, 2, 3, 5, 7, 9, 3, 2};
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i =0; i<x.length; i++)
{
if(map.containsKey(x[i]))
{
int t = map.get(x[i]) + 1;
map.put(x[i], t);
}
else
{
map.put(x[i], 1);
}
}
System.out.println(map);
}
}

OUTPUT :-
{2=4, 3=2, 5=2, 7=1, 9=2}

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

public class Interviewtest
{
//find the duplicate elements in the array
int count;
public static void main(String args[])
{
int arr[] = {4, 2, 2, 4, 55, 6, 99, 55};
AtomicInteger count = new AtomicInteger(1);
Set newarray = new HashSet();
for(int i =0; i<arr.length; i++)
{
if(newarray.contains(arr[i]))
{
int x = count.getAndIncrement();
//System.out.println("Element"+ " " + arr[i] + " " + x);
}
else
{
newarray.add(arr[i]);
}
for(int j =i+1; j<arr.length; j++)
{
if(arr[i]==arr[j])
{
System.out.println("Duplicate element is:->" +" "+arr[i]);
}
}
}
System.out.println(newarray);
g();
h();
}
public static void g()
{
String arr[] = new String[]{"chandan", "adhiraj", "Tina", "Cathy", "chandan", "adhiraj"};
Set duplicate = new HashSet<>();
Set nonduplicate = new HashSet<>();
for(String srting : arr)
{
/*if(!nonduplicate.contains(srting))
{
nonduplicate.add(srting);
}
else
{
duplicate.add(srting);
}*/
if(nonduplicate.contains(srting))
{
duplicate.add(srting);
}
else
{
nonduplicate.add(srting);
}
}
System.out.println(duplicate);
System.out.println(nonduplicate);
}
public static void h()
{
int arr[] = {4, 2, 2, 4, 55, 6, 99, 55};
for(int i =0; i<arr.length; i++)
{
int t = 1;
for(int j =i+1; j<arr.length; j++)
{
if(arr[i]==arr[j])
{
t = t+1;
}
}
System.out.println("Element" + " " + arr[i] + " " + "occurs" + " " + t);
}
}
}