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
What is instance example?
How to split arraylist elements in java?
State the significance of public, private, protected class?
What is a java list?
Define how destructors are defined in java?
What is the difference between array and array list in java?
What is the use of coding?
What is substring in java?
What is sizeof in java?
What is diamond operator in java?
Are there structures in java?
What are the java ide's? Explain
What are the two parts of a conditional statement?
What are exceptions
What are implicit objects in java?