How to eliminate duplicates from an array?

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What is a constructor overloading in java?

515


Tell me how many ways are there to initialise an integer with a constant.

636


What is a wrapper method?

531


What is the purpose of javac exe?

555


What does 0 mean in boolean?

537






Why do we need autoboxing in java?

513


What are local variables?

589


What is structure of java heap? What is perm gen space in heap?

538


How do you sort data in java?

518


Why strings in java are called as immutable?

589


How concurrent hashmap works?

605


What are daemon Threads in java?

606


What is the benefit of inner classes in java?

600


What are the advantages of functions?

517


What is stream api in java8?

533