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

What is the purpose of a statement block?

1 Answers  

Why Static variable required in java?For ex,class A { static int a; int b; } Why static is required?

5 Answers   Ericsson,

what is the use of bean managed and container managed with example?

0 Answers   EDS,

what is difference between abstraction and interface?

8 Answers   FIC,

Hi ,i convert contrller as jSp And presentation as servlet ...will it do? if so what are advantage and idsadvantages

2 Answers   IBM,

How to Sort list of Strings in ascending order without using java api.

1 Answers   NIIT, HCL,

is it mandatory to deaclare all variables public static fianl in interfaces?if i declare like in the below program, public interface A { public static final int I=0; int j=0; } in interface A,what is the difference between I,j?

2 Answers  

can write code for serialization ?

1 Answers   HCL, MegaSoft,

How 'java' got its name and what it stands for?

11 Answers   Wipro,

what is the difference between static class and singleton class? can we create static class?

2 Answers   L&T, Octazen, Vamsi Labs,

What is static and a non-static inner class?

3 Answers  

what r callable statement and give their proper use

2 Answers