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 a constructor overloading in java?
Tell me how many ways are there to initialise an integer with a constant.
What is a wrapper method?
What is the purpose of javac exe?
What does 0 mean in boolean?
Why do we need autoboxing in java?
What are local variables?
What is structure of java heap? What is perm gen space in heap?
How do you sort data in java?
Why strings in java are called as immutable?
How concurrent hashmap works?
What are daemon Threads in java?
What is the benefit of inner classes in java?
What are the advantages of functions?
What is stream api in java8?