How to eliminate duplicates from an array?

Answers were Sorted based on User's Feedback



How to eliminate duplicates from an array?..

Answer / manju

by using HashSet(Collection name)

Is This Answer Correct ?    33 Yes 1 No

How to eliminate duplicates from an array?..

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

How to eliminate duplicates from an array?..

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 : " +
Arrays.toString(data));

//
// 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
array.
//
String[] result = new String[set.size()];
set.toArray(result);
for (String s : result) {
System.out.print(s + ", ");
}
}
}

Is This Answer Correct ?    5 Yes 1 No

How to eliminate duplicates from an array?..

Answer / gajendra

using hash map or treeset of collection framework

Is This Answer Correct ?    8 Yes 5 No

How to eliminate duplicates from an array?..

Answer / vijaya bhaskar teegala

use linkedHashSet.It will maintain the insertion order

Is This Answer Correct ?    1 Yes 1 No

How to eliminate duplicates from an array?..

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){
list.add(str[i]);

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

}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

What is the epoch date?

0 Answers  


What is integer valueof?

0 Answers  


why string is not taking as primitive datatypes but we r taking it as a class? why explain it?

2 Answers   HP, SparkTG,


Why strings in java are called as immutable?

0 Answers  


difference between vectorlist and hash

1 Answers   TCS,






import java.io.*; class Demo { public static void main(String args[]) { File f=new File("1234.msg"); String arr[]=f.list(); System.out.println(arr.length); } }

3 Answers   IBM, Ramco,


Why should I use abstract class?

0 Answers  


What is backdrop?

0 Answers   Atos Origin,


Which is the best way to use for String concatenation in Java?

7 Answers   HCL,


How core java/j2ee project performance can be measured ?

1 Answers  


What is meant by constructor?

8 Answers  


Which container method is used to cause a container to be laid out and redisplayed in java programming?

0 Answers  


Categories