cybersecurity · intermediate · ~15 min · safe pentest lab

Distinct ports touched

Count unique values.

Challenge

Count how many distinct ports a source touched — a burst across many distinct ports is the port-scan signature.

Task

Implement int distinct_ports(const int *ports, int n) that returns the number of distinct port numbers in the array.

Input

  • ports: an array of n port numbers the grader passes (may contain duplicates).
  • n: the array length.

Output

Returns int: the count of distinct values.

Example

distinct_ports({22, 80, 22, 443, 80}, 5)   ->   3   (22, 80, 443)

Edge cases

  • Repeated ports count once.

Input format

An int array ports of length n.

Output format

An int: the number of distinct port values.

Constraints

Duplicates count once.

Starter code

int distinct_ports(const int *ports, int n) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.