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
Can a method be static?
What is scope of a variable?
Explain about automatic type conversion in java?
What are synchronized methods and synchronized statements in java programming?
What is the difference between numeric and integer?
Is oracle charging for java?
Why javac is not recognized?
What is the use of beaninfo?
Is there a case when finally will not execute?
What is java thread dump, how can we get java thread dump of a program?
What does the three dot emoji mean?
what is the purpose of the wait(), notify(), and notifyall() methods? : Java thread
What about static nested classes in java?
Is array synchronized in java?
What are the three parts of a lambda expression?