+ 1

Write this Code section different

i already posted this , but my post had some mistakes public class Test {   public static void main(String... args) {     System.out.println(merge(new int[] { 1, 3, 4, 4, 4, 4, 4, 5 }, 0));   }      public static String merge(int[] ns, int i) {     if (ns == null || i >= ns.length) {       return "";     } ***START HERE with Code "re-writing"***     int p= 1;     int n= i;     do {       p *= ns[i];       n++;     } while (n < ns.length && ns[i] == ns[n]);     return p+ " " + merge(ns, n);   } }

20th Nov 2016, 5:40 PM
newName
4 Réponses
+ 2
Finally had some time to do it :-) public static String mergeStreams(int[] ns, int i) { if (ns == null || i >= ns.length) { return ""; } Map<Integer, Integer> intMap = Arrays.stream(ns).skip(i).boxed().collect(Collectors.groupingBy(Integer::intValue, Collectors.reducing(1, (a, b) -> a * b))); return intMap.values().stream().map(String::valueOf).collect(Collectors.joining(" ")); } As for performance, it is much slower this way.
21st Nov 2016, 4:23 PM
Roland
+ 1
cool, now i understand it :-) hmmm i will give it a shot in about 2 hours or so. have to watch something now :-D
20th Nov 2016, 7:10 PM
Roland
0
well this one has mistakes too. power & next is undefined. :-)
20th Nov 2016, 6:47 PM
Roland
0
yeah... I am such a noob lol ._. I defined them :) now it should be at least without mistakes
20th Nov 2016, 7:01 PM
newName