String Array Duplicates in Java


The challenge

You are given an array of strings and your task is to remove all consecutive duplicate letters from each string in the array.

For example:

  • dup(["abracadabra","allottee","assessee"]) = ["abracadabra","alote","asese"].
  • dup(["kelless","keenness"]) = ["keles","kenes"].

Strings will be lowercase only, no spaces. See test cases for more examples.

The solution in Java code

Option 1 (using StringBuilders):

class Solution{    
    public static String[] dup(String[] arr){
        String[] strings = new String[arr.length];

        for (int h = 0; h < arr.length; h++) {

            String str = arr[h];

            StringBuilder sb = new StringBuilder();
            char current = 0;
            char last = 0;
            for (int i = 0; i < str.length(); i++) {
                current = str.charAt(i);
                if (current!=last) sb.append(current);
                last = current;
            }
            strings[h] = sb.toString();

        }

        return strings;
    }
}

Option 2 (reusing the input array):

class Solution{    
    public static String[] dup(String[] arr){
        for(int i = 0; i < arr.length; i++)
            arr[i] = arr[i].replaceAll("(.)\\1+", "$1");
        return arr;
    }
}

Option 3 (using Stream.of):

import static java.util.stream.Stream.of;

interface Solution {
  static String[] dup(String[] arr) {
    return of(arr).map(s -> s.replaceAll("(\\w)\\1+", "$1")).toArray(String[]::new);
  }
}

Test cases to validate our solution

import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import org.junit.runners.JUnit4;
import java.util.*;

public class SolutionTest{
    private static Random random = new Random();    
    
    private static String unDup1(String s){
        String res = "" + s.charAt(0);
        for (int i = 1; i < s.length(); i++)        
            if (s.charAt(i-1) != s.charAt(i)) res += s.charAt(i); 
        return res;
    }

    private static String[] bvy9(String[] arr){
        String [] res = new String[arr.length]; 
        for (int i = 0; i < res.length; ++i)
          res[i] = unDup1(arr[i]);
        return res;        
    }
    
    private static int random(int l, int u){
        return random.nextInt(u-l)+l;
    }
    
    @Test
    public void basicTests(){     
        assertArrayEquals(new String[]{"andrew","picaniny","hubububo"},Solution.dup(new String[]{"aaannnndddrreew","piccaninny","hubbubbubboo"}));
        assertArrayEquals(new String[]{"abracadabra","alote","asese"},Solution.dup(new String[]{"abracadabra","allottee","assessee"}));
        assertArrayEquals(new String[]{"keles","kenes"},Solution.dup(new String[]{"kelless","keenness"}));
       
    }
    
    @Test
    public void randomTests(){ 
        String abc = "abcdefghijklmnopqrstuvwxyzabcd";
        for(int k=0;k<100;k++){
            int arrLen = random(10,20);
            int len = random(10,20);
            int i = 0;
            String [] res = new String[arrLen];
            while (i < arrLen){
              String st = "";
              int j = 0;
              while (j < len){
                int ch  = random(0,26);
                st += abc.charAt(ch);
                if (ch % 2 == 0) st+=abc.charAt(ch);
                j++;
              }  
              res[i] = st;
              i++;
            }                  
            assertArrayEquals(bvy9(res),Solution.dup(res));
        }
    }
}