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 use of flag?
What are the difference between string, string builder, and string buffer in java?
Program to Find the second largest element in an array.
What are some examples of variable costs?
Can we override compareto method?
What is polymorphism and what are the types of it?
What is the exception hierarchy in java?
What is difference between float and double?
What is a byte string?
What are the three parts of a lambda expression?
What is arrays aslist in java?
What is the Scope of Static Variable?
Is void a wrapper class?
How do you clear an arraylist in java?
Assume a thread has lock on it, calling sleep() method on that thread will release the lock?