{ }codelper
list membershipP0059

Is it on the list?

The first line gives the list size n, the second line n integers (the list), the third line the number of queries q, and the fourth line q integers.
For each queried number, print "yes" if it is on the list and "no" otherwise, one per line.
The list may contain the same number more than once.

Input
1 ≤ n ≤ 1,000, 1 ≤ q ≤ 1,000, every number is an integer from -1,000,000,000 to 1,000,000,000
Output
q lines, each "yes" or "no"
Example 1 · stdin
3
10 20 300
4
20 1 300 30
stdout
yes
no
yes
no
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

Membership tools: Python x in a, JavaScript/TypeScript a.includes(x), C# a.Contains(x) (needs using System.Linq for arrays), C++ std::find(a.begin(), a.end(), x) != a.end() (C++23 adds std::ranges::contains), a Java array with Arrays.stream(a).anyMatch(v -> v == x), and in C a small loop function.
In Craykon it is a.Contains(x).
Three traps: (1) searching for the number as text also matches pieces: in the line "10 20 300", "1" and "30" are found.
(2) In Java, Arrays.asList(a).contains(x) with an int[] makes a List<int[]> (a list holding one array), so it is always false, and it still compiles.
(3) Putting else return 0 inside the loop gives up after the first item; you can only say "not there" after checking everything.
All of these scan from the front, so each query looks at up to n items; for very many queries use a set such as Python set or Java HashSet.

Pythonmain.py · 5 lines
n = int(input())a = list(map(int, input().split()))q = int(input())for x in map(int, input().split()):    print("yes" if x in a else "no")
JavaScriptmain.js · 5 lines
const nums = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const n = nums[0];const a = nums.slice(1, 1 + n);const queries = nums.slice(2 + n);console.log(queries.map((x) => (a.includes(x) ? "yes" : "no")).join("\n"));
TypeScriptmain.ts · 6 lines
import { readFileSync } from "node:fs";const nums: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const n: number = nums[0];const a: number[] = nums.slice(1, 1 + n);const queries: number[] = nums.slice(2 + n);console.log(queries.map((x) => (a.includes(x) ? "yes" : "no")).join("\n"));
JavaMain.java · 19 lines
import java.util.Arrays;import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int n = sc.nextInt();        int[] a = new int[n];        for (int i = 0; i < n; i++) a[i] = sc.nextInt();        int q = sc.nextInt();        StringBuilder out = new StringBuilder();        for (int k = 0; k < q; k++) {            int x = sc.nextInt();            boolean found = Arrays.stream(a).anyMatch(v -> v == x);            out.append(found ? "yes" : "no").append('\n');        }        System.out.print(out);    }}
C#Main.cs · 17 lines
using System;using System.Linq; class Program{    static void Main()    {        Console.ReadLine();        int[] a = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();        Console.ReadLine();        foreach (string s in Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries))        {            int x = int.Parse(s);            Console.WriteLine(a.Contains(x) ? "yes" : "no");        }    }}
C++main.cpp · 16 lines
#include <algorithm>#include <iostream>#include <vector> int main() {    int n, q, x;    std::cin >> n;    std::vector<int> a(n);    for (int& v : a) std::cin >> v;    std::cin >> q;    for (int k = 0; k < q; k++) {        std::cin >> x;        bool found = std::find(a.begin(), a.end(), x) != a.end();        std::cout << (found ? "yes" : "no") << "\n";    }}
Cmain.c · 19 lines
#include <stdio.h> int contains(const int *a, int n, int x) {    for (int i = 0; i < n; i++)        if (a[i] == x) return 1;    return 0;} int main(void) {    int n, q, x, a[1000];    scanf("%d", &n);    for (int i = 0; i < n; i++) scanf("%d", &a[i]);    scanf("%d", &q);    for (int k = 0; k < q; k++) {        scanf("%d", &x);        puts(contains(a, n, x) ? "yes" : "no");    }    return 0;}
Craykonmain.cray · 7 lines
n = Console.ReadLine().ToNumber();a = Console.ReadNumbers();q = Console.ReadLine().ToNumber();foreach (x in Console.ReadNumbers()){    Console.WriteLine(a.Contains(x) ? "yes" : "no");}