[精讚] [會員登入]
496

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

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

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

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

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

import csv into jira to create mass issues import csv into jira to create mass issues

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