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 the ==?
What is difference between static class and normal class?
Which is illegal identifier in java?
What is super?
Which is bigger float or double java?
How does a for loop work?
In a class implementing an interface, can we change the value of any variable defined in the interface?
What is polymorphism java example?
Explain the key functions of data binding?
Can java list be null?
Is jdk required on each machine to run a java program?
Is vector synchronized in java?
What is the primitive type short?
What are the steps in the jdbc connection?
Is there any way to skip finally block of exception even if some exception occurs in the exception block?