1 2 Previous Next 18 Replies Latest reply: Sep 12, 2008 1:07 AM by 800282 Go to original post Branched to a new discussion. RSS
      • 15. Re: Remove Duplicates from String Array with Regular Expressions
        807589
        Talking of uncle_alice, I haven't seen him around for quite a while!
        Unlike me, he probably has to do some real work.
        Naw, it's just that you guys keep beating me to the juicy regex questions. :-/
        • 16. Re: Remove Duplicates from String Array with Regular Expressions
          800282
          uncle_alice wrote:
          ...
          Naw, it's just that you guys keep beating me to the juicy regex questions. :-/
          Well, you can always show of with a more mind-boggling solution than one of us proposed!
          ; )
          • 17. Re: Remove Duplicates from String Array with Regular Expressions
            807589
            I thought you were the designated boggler these days. But if you insist... :D
            import java.util.*;
            import java.util.regex.*;
            
            public class Test
            {
              public static void main(String... args) throws Exception
              {
                String[] array = {"1","10","1","10","2","2","1","3","3"};
                System.out.println(Arrays.toString(array));
            
                String temp = Arrays.toString(array).replaceAll("[\\s\\[\\]]", "");
            
                String regex = String.format("(?=(\\b\\d++\\b))(?<=(?=\\b\\1\\b).{1,%d})\\1,?", temp.length());
            
                array = temp.replaceAll(regex, "").split(",");
            
                System.out.println(Arrays.toString(array));
              }
            }
            output:
            [1, 10, 1, 10, 2, 2, 1, 3, 3]
            [1, 10, 2, 3]
            As Fred pointed out, the OP specified that the order of the elements was to be preserved, which means the first occurrence of each value is to be retained, not the last. This approach preserves the order.
            • 18. Re: Remove Duplicates from String Array with Regular Expressions
              800282
              uncle_alice wrote:
              I thought you were the designated boggler these days. But if you insist... :D
              I try to contribute, yes.
              >     ... 
              
              String regex = String.format("(?=(\\b\\d++\\b))(?<=(?=\\b\\1\\b).{1,%d})\\1,?", temp.length());
              ...
              }
              }
               
              ...
              ; )
              That IS a mind-boggler!
              1 2 Previous Next