0

So I have String array that looks like this:

arraySubs:

math
math
math
web
web
web
prog
prog
prog

Now I want to remove the duplicates to look like this:

arraySubs:

math
web
prog

I wouldn't mind if there is null on places that should be removed, so I tried this:

for(int j = 0; j < arraySubs.length; j++) {
    if(j<arraySubs.length-1) {
        if(arraySubs[j+1]==arraySubs[j]) {//equalsIgnoreCase doesn't work.
            arraySubs[j]=null;
        }   
    }
    if(arraySubs[j]!=null) {
        System.out.println(arraySubs[j]);
    }
}

But it doesn't work it's still printing all of them, any ideas? I don't wanna use Set, HashSet etc. Or any other tools such as iterators. equals() doesn't work...

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Kakarot
  • 43
  • 8
  • 3
    Don't compare Strings with `==` – Eran Sep 06 '17 at 08:53
  • why do you want to inhibit yourself too much? Can you loop and add to a List if it does not exist? Or even append to a String with a delimiter and then check for the existence? – Scary Wombat Sep 06 '17 at 08:53
  • You only ever compare each item against exactly one other. You need to add an inner loop for searching. – daniu Sep 06 '17 at 08:54
  • (A) Do you know that the array is ordered? (B) Why `equalsIgnoreCase` doesn't work? What is the output you get in this case? – Roee Gavirel Sep 06 '17 at 08:57
  • This is actually a good example on when to use ArrayLists. It would save you a lot of unncessary trouble in that case – Alexander Heim Sep 06 '17 at 08:57
  • @RoeeGavirel (A)Yes,(B) I caught nullPointerException.... – Kakarot Sep 06 '17 at 09:02
  • @GhostCat its not duplicate question, equals doesn't help, please remove the mark for dupicates. – Kakarot Sep 06 '17 at 09:03
  • @DusanMartinovicFit then you have other problem, `==` for strings definitely wouldn't work. – Roee Gavirel Sep 06 '17 at 09:05
  • @GhostCat , I edited my code , so now it shows problem with equals(), is this good? – Kakarot Sep 06 '17 at 09:11
  • Don't get frustrated ... but now your question is a DUP of [this](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) question. And beyond that: dont put up *parts* of your code. Instead - read [mcve] and give *clear* descriptions of expected and actual output. But still: you are dealing with *very basic* problems. And this community is not a free tutor service where you get taught such basic stuff. – GhostCat Sep 06 '17 at 09:13

3 Answers3

4

If you don't want to use set you can use stream:

list.stream()
    .distinct()
    .collect(Collectors.toList());

But still, using set will be the most efficient and clear.

ByeBye
  • 6,650
  • 5
  • 30
  • 63
1

You can use stream with collect. This way, you will even be able to maintain the counts for each element, e.g.:

String[] array = new String[]{"math", "math", "web"};
Map<String, Long> items = Arrays.stream(array)
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(items);
System.out.println(items.keySet());
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
0

When you do arraySubs[j+1]==arraySubs[j] you compare instances ids rather then value. You should use arraySubs[j+1].equals(arraySubs[j])

Just try to debug your code, or run something like:

for(int j = 0; j < arraySubs.length; j++) {
    if(j<arraySubs.length-1) {
        System.out.println("j:" + j);
        System.out.println("j val:" + arraySubs[j]);
        System.out.println("j+1 val:" + arraySubs[j+1]);
        if(arraySubs[j+1] != null && arraySubs[j+1].equalsIgnoreCase(arraySubs[j])) { // The FIX
            System.out.println("j val changed to null");
            arraySubs[j]=null;
        }   
    }
    if(arraySubs[j]!=null) {
        System.out.println(arraySubs[j]);
    } else {
        System.out.println("skipping null");
    }
}
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94