[精讚] [會員登入]
766

to implement a difference function, which subtracts one list from another and returns the result.

Description: Your goal in this kata is to implement a differ

分享此文連結 //n.sfs.tw/15349

分享連結 to implement a difference function, which subtracts one list from another and returns the result.@der amateur
(文章歡迎轉載,務必尊重版權註明連結來源)
最後編修
2021-08-22 15:11:10 By
 
Description:

Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.

It should remove all values from list a, which are present in list b keeping their order.

example:

Kata.arrayDiff(new int[] {1, 2}, new int[] {1}) => new int[] {2}

If a value is present in b, all of its occurrences must be removed from the other:

 

best solution:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Kata {
    public static int[] arrayDiff(int[] a, int[] b) {
        List<Integer> listA = Arrays.stream(a).boxed().collect(Collectors.toList());
        List<Integer> listB = Arrays.stream(b).boxed().collect(Collectors.toList());
        listA.removeAll(listB);
        return listA.stream().mapToInt(e -> e).toArray();
    }
}

 

END

你可能感興趣的文章

a function that takes an array of integers and a target then added any two equals the target Write a function that takes an array of numbers (integers fo

JAVA Maximum Length Difference You are given two arrays a1 and a2 of strings. Each string i

find all elements in listA belonging to each element of listB and to sum according to listA A bookseller has lots of books classified in 26 categories l

a function which takes a list of strings and returns each line prepended by the correct number Write a function which takes a list of strings and returns e

to implement a difference function, which subtracts one list from another and returns the result. Description: Your goal in this kata is to implement a differ

隨機好文

how to deal with ubuntu 18.04 network issues bring up different ethernet interfaces on installed ubuntu18.04, then fix yaml indentation problem

a function that takes an array of integers and a target then added any two equals the target Write a function that takes an array of numbers (integers fo

packages before adding as module into Intellij IDEA project, need to handle git repostory first. if you wish doing VCS for your code packages, do git setup first, before adding them into Intellij IDEA