ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Count Nodes with Specific Property

Cypher (Neo4j)

Goal -- WPM

Ready
Exercise Algorithm Area
1MATCH (n:Person {city: $cityName})
2RETURN count(n) AS personCount
Algorithm description viewbox

Count Nodes with Specific Property

Algorithm description:

This query counts the number of nodes with the label 'Person' that also have a 'city' property matching a given value. It's a fundamental operation for understanding the distribution of entities within a graph. For example, it could be used to count all customers in a specific city.

Algorithm explanation:

The query uses a simple `MATCH` clause to find nodes with both a label ('Person') and a specific property value ('city' equal to `$cityName`). The `count(n)` aggregation then returns the total number of such nodes. This is a very efficient operation, typically O(N) in the worst case where N is the number of nodes with the 'Person' label, but often much faster if the 'city' property is indexed, approaching O(1) or O(log N) depending on index performance. Edge cases include no nodes matching the criteria (returns 0) or an empty graph.

Pseudocode:

1. Match nodes with a specific label and property value.
2. Count the matched nodes.
3. Return the count.