# Comparing sequence length

**URL:** <https://discuss.kotlinlang.org/t/comparing-sequence-length/29387>\
**Category:** Support\
**Created:** [October 11, 2024, 3:10pm UTC](https://discuss.kotlinlang.org/t/comparing-sequence-length/29387 "2024-10-11T15:10:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjoerdvogel](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@sjoerdvogel](https://discuss.kotlinlang.org/u/sjoerdvogel)\
**Post date:** [October 11, 2024, 3:10pm UTC](https://discuss.kotlinlang.org/t/comparing-sequence-length/29387/1 "2024-10-11T15:10:07Z")

</div>

I am trying to figure out the best way to compare sequence length, lets say

```kotlin
mySequence.count() > 1

```

It seems to me this will likely iterate over the entire sequence. This seems wasteful especially if the sequence is large. Therefore, I came up with

```kotlin
mySequence.windowed(2).any()

```

Does this make sense?

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [October 11, 2024, 3:17pm UTC](https://discuss.kotlinlang.org/t/comparing-sequence-length/29387/2 "2024-10-11T15:17:07Z")

</div>

Sequence doesn’t know its length, this is the main point of sequences that differs them from collections.

---

<div class="post-metadata">

**Author:** ![sjoerdvogel](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@sjoerdvogel](https://discuss.kotlinlang.org/u/sjoerdvogel)\
**Post date:** [October 11, 2024, 3:17pm UTC](https://discuss.kotlinlang.org/t/comparing-sequence-length/29387/3 "2024-10-11T15:17:43Z")

</div>

I get this, but I sometimes want to verify a minimum length.

It is also my understanding that it processes elements one by one (like java streams) as opposed to map etc methods on collections.

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [October 11, 2024, 3:23pm UTC](https://discuss.kotlinlang.org/t/comparing-sequence-length/29387/4 "2024-10-11T15:23:03Z")

</div>

So you need to only check if `count()` is at least `min` and you accept iterating through first `min` items in order to check it? I think the most straightforward is: `seq.drop(min - 1).any()`.

---

<div class="post-metadata">

**Author:** ![sjoerdvogel](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@sjoerdvogel](https://discuss.kotlinlang.org/u/sjoerdvogel)\
**Post date:** [October 11, 2024, 3:24pm UTC](https://discuss.kotlinlang.org/t/comparing-sequence-length/29387/5 "2024-10-11T15:24:02Z")

</div>

That is exactly right. Would drop perform better then windowed? Also, what would drop do if the sequence is empty?

_edit_  
It seems drop does indeed do nothing on an empty sequence, or just completely empties it if n is much larger then the sequence size. So this would indeed work. So I guess that leaves the question of performance and/or esthetic preference.

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [October 11, 2024, 7:30pm UTC](https://discuss.kotlinlang.org/t/comparing-sequence-length/29387/6 "2024-10-11T19:30:20Z")

</div>

`windowed` may be potentially slower, because it has to create the window - it creates a buffer and puts items into it, even if you are entirely not interested in these items. `drop` simply skips items. In practice, difference is probably neglectable.

edit:  
But at least for me `windowed` is much less readable. It took me some time to understand what you actually did there. It requires understanding `windowed` logic and that it skips partial windows, etc. If you look for the most readable way, then maybe: `seq.take(min).count() == min`. Even better, simply create an extension `countAtLeast(Int)` and then you can use whatever you consider the fastest.

---

<div class="post-metadata">

**Author:** ![sjoerdvogel](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@sjoerdvogel](https://discuss.kotlinlang.org/u/sjoerdvogel)\
**Post date:** [October 12, 2024, 12:02pm UTC](https://discuss.kotlinlang.org/t/comparing-sequence-length/29387/7 "2024-10-12T12:02:24Z")

</div>

Yeah, I was considering maybe creating an extension. I had something much more complicated (tinkering with the iterator) first, but IMO both

```kotlin
seq.windowed(min).any()

```

```kotlin
seq.drop(min-1).any()

```

```kotlin
seq.take(min).count() == min

```

seem too compact to warrant a public reusable extension. Maybe I could use a private one and scope it to the class in question?  
That last one is an interesting variant btw. I think all three of these won’t iterate the entire sequence and are all likely better compared to doing that. After all this discussion I’m leaning towards the second (drop) one.
