package com.test.blogger;
/**
* @author EducateJava
*/
public class RemoveDuplicatesFromArray {
public static void main(final String[] args) {
final RemoveDuplicatesFromArray removeDuplicatesFromArray = new RemoveDuplicatesFromArray();
final String[] finalArray = removeDuplicatesFromArray
.removeDup(new String[] { "1", "one", "1", "three", "two", "2", "three", "1" });
for (final String obj : finalArray) {
System.out.println(obj);
}
}
public String[] removeDup(final String[] stringArray) {
boolean matched = false;
final String[] res = new String[stringArray.length];
for (int i = 0; i < stringArray.length; i++) {
res[i] = "";
}
int counter = 0;
for (final String element : stringArray) {
for (final String re : res) {
if (element == re) {
matched = true;
break;
} else {
matched = false;
}
}
if (!matched) {
res[counter] = element;
counter++;
}
}
return res;
}
}
Output:
=======
1
one
three
two
2
No comments :
Post a Comment