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 static block?
What is the default value of float and double datatype in java?
What are the principle concepts of oops?
How do you create a first line indent?
What is entry in java?
What is a two-pass assembler?
I want to persist data of objects for later use. What’s the best approach to do so?
What language is java written?
What exactly is methodology?
Hi Anyone know the model / questions of the Federal bank sample questions for the post of Specialist Officers - Programmers. Please post if anyone have..
What is the difference between declaration and definition in java?
Is null in java?
List down the methods and interfaces of collection class in java.
What is a nested class?
What is java english?