One potential solution1 is to add all the items to a List
, sort the list, and then iterate over the list checking if the first n characters are the same as the previous entry, and if not, print it.
First, we'd need to get the third index of .
in this scenario2:
public static int nthIndexOf(String text, char needle, int n) {
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == needle) {
n--;
if (n == 0) {
return i;
}
}
}
return -1;
}
Then simply perform the iteration as mentioned above:
for (int i = 1; i < list.size(); i++) {
int pos = nthIndexOf(list.get(i), '.', 3);
if (!list.get(i).substring(0, pos).equals(list.get(i - 1).substring(0, pos))) {
System.out.println(list.get(i));
}
}
Oh, and just print the first entry too, as it's going to be unique, considering it hasn't been compared to anything yet.
Of course, I am just printing System.out.println(list.get(i));
, but do whatever is necessary with it there.
Example
1There may be edge cases I've missed, but that's up to you to check
2Credit where credit is due