Kata of the Day #20

Array diff

kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
Kata of the Day #20

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

TypeScript


function arrayDiff(a: number[], b: number[]): number[] {
    return a.filter(e => !b.includes(e));
}

console.log(arrayDiff([3, 4], [3]));

Go


package main

import (
	"fmt"
)

func arrayDiff(a []int, b []int) []int {
	diff := []int{}
	for _, e := range a {
		if !contains(b, e) {
			diff = append(diff, e)
		}
	}
	return diff
}

func contains(arr []int, val int) bool {
	for _, el := range arr {
		if el == val {
			return true
		}
	}
	return false
}

func main() {
	fmt.Println(arrayDiff([]int{3, 4}, []int{3}))
}

Python


def array_diff(a, b):
    return [e for e in a if e not in b]

print(array_diff([3, 4], [3]))

PHP


function arrayDiff($a, $b) {
    return array_filter($a, function($e) use ($b) {
        return !in_array($e, $b);
    });
}

echo print_r(arrayDiff([3, 4], [3]));

Go to Kata
ByCesar Kohl

Need some help?

Want us to give you some help with your business? No problem, here's a video on who we can help, and how, along with our calendar so you can book in a call to discuss us working together.

Let's see
budget
budget
budget
budget
budget
budget
budget
budget
budget
budget

Subscribe to our newsletter

subscribe
subscribe
subscribe
subscribe
subscribe
subscribe
subscribe
subscribe
subscribe
subscribe