[精讚] [會員登入]
597

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

你可能感興趣的文章

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

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

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 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

隨機好文

how to install teamviewer on ubuntu linux 18.04 LTS install teamviewer by using deb file

how to rename git branch rename git branch

JIRA instance migration phase3: upgrading jira trying to upgrade jira from 7.6.1 to 8.x