-2

I want to remove same subnet proxies from a text file.

txt.file= proxy.txt

19.15.15.90:61234

19.15.15.29:28010

19.15.15.80:8998

19.15.15.102:8998

25.25.24.15:8998

25.25.24.80:8998

210.192.38.25:8998

210.192.38.29:8998

I need output be

19.15.15.90:61234

25.25.24.15:8998

210.192.38.25:8998

It doesn't matter which proxies are removed, I just needs to keep 1 from each subnet.

subnet= first 3 numbers the same.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Local Host
  • 41
  • 5

1 Answers1

0

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

achAmháin
  • 4,176
  • 4
  • 17
  • 40
  • maybe sounds too stupid but, where i enter the list? – Local Host May 09 '18 at 17:48
  • https://stackoverflow.com/questions/5343689/java-reading-a-file-into-an-arraylist If you want to read your file into a list, read this. Or click “Example” near the bottom of my post. – achAmháin May 09 '18 at 17:56