How to eliminate duplicates from an array?

Answers were Sorted based on User's Feedback

Answer / manju

by using HashSet(Collection name)

Is This Answer Correct ?    33 Yes 1 No

Answer / edward sudhaharchennai

Unlike other implementations of the collection interface,
implementation of the Set intrface do not allow duplicate
elements. This also means that a set can contain at most
one null value. The set interface does not define any new
methods, and its add() and addAll() methods will not store
duplicates. If an element is not currently in the set, two
consecutive calls to the add() method to insert the element
will first return true then false...........

Is This Answer Correct ?    6 Yes 2 No

Answer / gajendra

using hash map or treeset of collection framework

Is This Answer Correct ?    8 Yes 5 No

Answer / qim2010

Using HashSet class we can eliminate duplicates from and
array. Here is a simple example

public class ArrayRemoveDuplicate {
public static void main(String[] args) {
// A string array with duplicate values
String[] data = {"A", "C", "B", "D", "A", "B", "E",
"D", "B", "C"};
System.out.println("Original array : " +

// Convert it to list as we need the list object to
create a set object.
// A set is a collection object that cannot have a
duplicate values, so
// by converting the array to a set the duplicate
value will be removed.
List<String> list = Arrays.asList(data);
Set<String> set = new HashSet<String>(list);

System.out.print("Remove duplicate result: ");

// Create an array to convert the Set back to array.
The Set.toArray()
// method copy the value in the set to the defined
String[] result = new String[set.size()];
for (String s : result) {
System.out.print(s + ", ");

Is This Answer Correct ?    4 Yes 1 No

Answer / vijaya bhaskar teegala

use linkedHashSet.It will maintain the insertion order

Is This Answer Correct ?    1 Yes 1 No

Answer / elango boopathy

package com.sample.pack;

import java.util.ArrayList;
import java.util.List;

public class Duplicates {

* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub
int k = 1;
String[] str = { "abc", "123", "tyu", "xyz", "123", "m",
"abc", "abc" };
boolean isDuplicate = false;
List<String> list = new ArrayList<String>();

for (int i = 0; i < str.length; i++) {
for (int j = k; j < str.length; j++) {
if (str[i].equals(str[j].toString())) {
isDuplicate = true;
k = k + 1;
if(isDuplicate == false){

isDuplicate = false;
Object[] afterDuplicate = list.toArray();
for(int i=0; i<afterDuplicate.length; i++){


Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

I want to run a simple hello world java ( program using a batch file. How can i run it and how to construct a batch file.

1 Answers   Infosys,

Explain about global variables in Java?

3 Answers  

How to extract HashMap values?

5 Answers   Marlabs,

What are Font and FontMetrics classes?

1 Answers  

what is object deep copy and shallow copy and why it is required?

2 Answers  

what is default constructor and parameterised constructor with example?

1 Answers   KPIT,

class A { class B { psvm(String args[]) { } } } if the prg saved in whats the o/p?

7 Answers   TCS, HCL,

how to handle exceptions in ejb?

0 Answers   Satyam,

what is collatration?

0 Answers  

write a program to create an vector and listeterator.and value should be enter through keyboard.

1 Answers   Axcend,

Hi am an mca graduate . i have done bsc maths in my degree . every company asks me why you make shift from maths group to computere field . What i need to answer?

2 Answers  

Java openings 3 - 5 years, Lnt Infotech. requirements - core java, J2ee, struts, hibernate Interview Date:- 19 March 2011 Time:- 9:00 AM to 12:00 Pm Interview Location - L & T Infotech, Manapakkam, Chennai Refererral PS NO:- 291649 (Please mention this when u fill the form only then u will be considered for interview) Documents Required:- Latest Resume, Photograph and last 3 payslips Mail me on once u appear for interview

0 Answers   L&T,