[精讚] [會員登入]
539

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

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

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

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

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

how to rename git branch rename git branch

steps to pass-through usb smart card reader for virtualbox how to pass through usb-smart-card-reader from ubuntu host to virtualbox vm

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