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

Can a method be static?

724


What is scope of a variable?

820


Explain about automatic type conversion in java?

814


What are synchronized methods and synchronized statements in java programming?

784


What is the difference between numeric and integer?

719


Is oracle charging for java?

768


Why javac is not recognized?

705


What is the use of beaninfo?

783


Is there a case when finally will not execute?

768


What is java thread dump, how can we get java thread dump of a program?

769


What does the three dot emoji mean?

799


what is the purpose of the wait(), notify(), and notifyall() methods? : Java thread

772


What about static nested classes in java?

812


Is array synchronized in java?

778


What are the three parts of a lambda expression?

728