Check if element exists in zig array

629 Views Asked by At

Is there a more idiomatic way of finding if some value is contained in an array than

fn valueInArray(value: u32, array: []const u32) bool {
    for (array) |num| {
        if (value == num) {
            return true;
        }
    }
    return false;
}
1

There are 1 best solutions below

0
On

I think that this is generally the most idiomatic solution. It is possible to use containsAtLeast from std.mem, but this seems like an abuse of that function (which is really for finding sequences):

if (std.mem.containsAtLeast(u32, &my_arr, 1, &[_]u32{42})) {
    std.debug.print("Found it!\n", .{});
} else {
    std.debug.print("No secrets found...\n", .{});
}

But I think that the OP posted code could be made more idiomatic by using comptime to allow it to accept types other than u32:

const std = @import("std");

pub fn inSlice(comptime T: type, haystack: []const T, needle: T) bool {
    for (haystack) |thing| {
        if (thing == needle) {
            return true;
        }
    }
    return false;
}

pub fn main() void {
    const my_nums = [_]u32{ 1, 2, 3, 42, 5 };
    const my_chars = [_]u8{ 'a', 'b', 'c', 'X', 'd' };

    // Check for a `u32` in a slice of `u32`:
    if (inSlice(u32, &my_nums, 42)) {
        std.debug.print("Found the secret!\n", .{});
    } else {
        std.debug.print("No secrets found...\n", .{});
    }

    // Check for a `u8` in a slice of `u8`:
    if (inSlice(u8, &my_chars, 'X')) {
        std.debug.print("'X' marks the spot!\n", .{});
    } else {
        std.debug.print("No treasure found...\n", .{});
    }

    // Check for a character in a string:
    if (inSlice(u8, "Marzipan", 'z')) {
        std.debug.print("Found some delicious marzipan!\n", .{});
    } else {
        std.debug.print("No treats for me...\n", .{});
    }
}
> .\find_in_array
Found the secret!
'X' marks the spot!
Found some delicious marzipan!