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


Please Help Members By Posting Answers For Below Questions

What do you mean by chromounits in java8?

520


Can you make a constructor final in Java?

618


What is args length in java?

650


What technique is carried out to find out if a particular string is empty?

557


Which package is imported by default?

621






What are the important features of Java 10 release?

506


What is Applet Stub Interface ?

1959


Can we have more than one package statement in the source file?

632


Explain importance of throws keyword in java?

558


How do you calculate roots in java?

516


Can singleton class be inherited in java?

509


What are keywords in programming?

556


What is anti pattern in java?

510


How hashset works internally in java?

576


How do you reverse a word in java?

523