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 do you mean by chromounits in java8?
Can you make a constructor final in Java?
What is args length in java?
What technique is carried out to find out if a particular string is empty?
Which package is imported by default?
What are the important features of Java 10 release?
What is Applet Stub Interface ?
Can we have more than one package statement in the source file?
Explain importance of throws keyword in java?
How do you calculate roots in java?
Can singleton class be inherited in java?
What are keywords in programming?
What is anti pattern in java?
How hashset works internally in java?
How do you reverse a word in java?