array_diff
array_diff(array $array, array ...$arrays): array
Computes the array difference: values in the first array that are absent from the others, preserving order and keys.
C C23
#include <stdio.h>
int main(void) {
int xs[] = {1, 2, 3, 4, 5};
int drop[] = {2, 4};
size_t n = sizeof xs / sizeof xs[0];
size_t m = sizeof drop / sizeof drop[0];
printf("[");
int first = 1;
for (size_t i = 0; i < n; i++) { // hand-written difference: nested membership scan
int found = 0;
for (size_t j = 0; j < m; j++) {
if (xs[i] == drop[j]) { found = 1; break; }
}
if (!found) {
printf(first ? "%d" : ", %d", xs[i]);
first = 0;
}
}
printf("]\n");
return 0;
}
[1, 3, 5]
C++ C++26
#include <iostream>
#include <vector>
int main() {
std::vector<int> xs = {1, 2, 3, 4, 5};
std::vector<int> drop = {2, 4};
std::vector<int> out;
for (int x : xs) {
bool found = false;
for (int d : drop) {
if (x == d) { found = true; break; }
}
if (!found) out.push_back(x);
}
std::cout << "[";
for (size_t i = 0; i < out.size(); ++i) {
if (i) std::cout << ", ";
std::cout << out[i];
}
std::cout << "]\n";
return 0;
}
[1, 3, 5]
C# v11.0.100
var xs = new List<int> { 1, 2, 3, 4, 5 };
var diff = xs.Except(new[] { 2, 4 }).ToList(); // LINQ set difference (also dedupes)
Console.WriteLine($"[{string.Join(", ", diff)}]");
[1, 3, 5]
Go v1.27.4
package main
import "fmt"
func main() {
a := []int{1, 2, 3, 4, 5}
drop := map[int]bool{2: true, 4: true} // map membership keeps it O(n)
var out []int
for _, v := range a {
if !drop[v] {
out = append(out, v)
}
}
fmt.Println(out)
}
[1 3 5]
Java v27.0.1
import java.util.Arrays;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<Integer> drop = Set.of(2, 4);
int[] out = Arrays.stream(new int[]{1, 2, 3, 4, 5})
.filter(x -> !drop.contains(x))
.toArray();
System.out.println(Arrays.toString(out));
}
}
[1, 3, 5]
JavaScript v26.13.1
const drop = new Set([2, 4]);
console.log([1, 2, 3, 4, 5].filter((x) => !drop.has(x))); // Set.has is O(1)
[ 1, 3, 5 ]
PHP v8.6.0
// array_diff keeps keys, so renumber with array_values for a clean JSON list
echo json_encode(array_values(array_diff([1, 2, 3, 4, 5], [2, 4])));
[1,3,5]
Python v3.15.2
print([x for x in [1, 2, 3, 4, 5] if x not in {2, 4}]) # set membership is O(1)
[1, 3, 5]
Rust v1.99.1
fn main() {
let xs = [1, 2, 3, 4, 5];
let drop = [2, 4];
let out: Vec<i32> = xs.iter().filter(|x| !drop.contains(x)).copied().collect();
println!("{:?}", out);
}
[1, 3, 5]
TypeScript v26.13.1
const drop: ReadonlySet<number> = new Set([2, 4]);
console.log([1, 2, 3, 4, 5].filter((x) => !drop.has(x)));
[ 1, 3, 5 ]
Language differences
C: has no such container method and needs a hand-written nested scan (or pre-sorting); the general performance idiom is a membership container (Set/map) turning the O(n×m) double scan into O(1) lookups.
C++: has no such container method and needs a hand-written nested scan (or pre-sorting).
C#: Except is a set difference — it dedupes the result too (invisible in this duplicate-free example).
Go: an order-preserving filter.
Java: Stream.filter, an order-preserving filter.
JavaScript: filter, an order-preserving filter.
PHP: array_diff compares with loose string equality (string)$a === (string)$b (variants: array_diff_key by keys, array_diff_assoc by key+value, array_diff_u* with callbacks), accepts multiple arrays, and keeps the original keys — hence the array_values wrapper to get a clean JSON list; JSON output formatting differs (PHP without spaces, JS console with spaces, Go without commas) as per-language serialization conventions, while the values agree.
Python: the list comprehension (order-preserving, keeps duplicates) matches PHP most closely; the set difference set(a) - set(b) dedupes and loses order, which is a different semantics.
Rust: an order-preserving filter.