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`):

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 `````` ``````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):

 ``````1 2 3 4 5 6 7 `````` ``````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`):

 ``````1 2 3 4 5 6 7 `````` ``````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

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 `````` ``````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)); } } } ``````