Finding duplicate element in ArrayList

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.spy.javaforlearn;
 
import java.util.ArrayList;
 
public class FindDuplicate 
{
 
	public static void main(String[] args) 
	{
 
         ArrayList<String> list=new ArrayList<String>();
 
		//Adding element into Arraylist
 
		list.add("Bhavesh");
 
		list.add("Abhi");
 
		list.add("vivek");
 
		list.add("milan");
 
		list.add("Bhavesh");
 
		list.add("vivek");
 
		 System.out.println("Before removing duplicate "+list);
 
		//Removing Duplicate
		for(int i=0;i<list.size();i++)
		{
 
			for(int j=i+1;j<list.size();j++)
			{
 
				if(list.get(i)==list.get(j))
				{
 
					list.remove(j);
				}
 
			}
 
 
		}
 
	  System.out.println("After removing duplicate "+list);
 
	}
}

Output:

1
2
Before removing duplicate [Bhavesh, Abhi, vivek, milan, Bhavesh, vivek]
After removing duplicate [Bhavesh, Abhi, vivek, milan]

Leave a Reply

Your email address will not be published. Required fields are marked *