# A doubt about SubsetCases

**URL:** <https://community.wolfram.com/t/a-doubt-about-subsetcases/16119>\
**Category:** Questions\
**Created:** [May 31, 2020, 7:59pm UTC](https://community.wolfram.com/t/a-doubt-about-subsetcases/16119 "2020-05-31T19:59:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![fgutiers2002](https://avatars.discourse-cdn.com/v4/letter/f/bb73d2/32.png) [@fgutiers2002](https://community.wolfram.com/u/fgutiers2002)\
**Post date:** [May 31, 2020, 7:59pm UTC](https://community.wolfram.com/t/a-doubt-about-subsetcases/16119/1 "2020-05-31T19:59:34Z")

</div>

Hello all. Version 12.1 has several very useful functions. SubsetCases seems to be one of them. However, I have a doubt regarding its use. It comes from an example from the documentation:

```
SubsetCases[{1, 2, 3, a, a, b, c}, {a, _Integer}]

```

This returns {{a,1},{a,2}}. Why not {a,3}? Indeed it has an overlap with the previous two lists, but the chosen output also has the same overlap (that is, the symbol a}

If indeed overlaps are turned to true {a,3} and much more is included.  
But if the overlaps are omitted, why does the code return {a,2} and not {a,3}?  
Any clarification is welcomed.  
Francisco

---

<div class="post-metadata">

**Author:** ![mikebesso](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mikebesso/32/22282_2.png) [@mikebesso](https://community.wolfram.com/u/mikebesso)\
**Post date:** [May 31, 2020, 9:46pm UTC](https://community.wolfram.com/t/a-doubt-about-subsetcases/16119/2 "2020-05-31T21:46:50Z")

</div>

I think your interpretation of the documentation is reasonable. In fact, your understanding matched mine until I started experimenting.

Assuming that the SubsetCases[] works as designed, “overlap” is based on the elements of the first argument.

To illustrate, adding an additional a gives you the results we both expected:

```
SubsetCases[{1, 2, 3, a, a, a, b, c}, {a, _Integer}]

```

returns:

```
{{a, 1}, {a, 2}, {a, 3}}

```

So, “overlap” seems to be controlling whether elements of the first list can be reused.

Unfortunately, allowing overlaps probably does not give what you want. For example:

```
SubsetCases[{1, 2, 3, a, a, b, c}, {a, _Integer}, Overlaps -> True]

```

returns

{{a, 1}, {a, 2}, {a, 3}, {a, 1}, {a, 2}, {a, 3}}

Fortunately, I think there is a fix that might work for you:

```
Union[SubsetCases[{1, 2, 3, a, a, b, c}, {a, _Integer}, 
  Overlaps -> True]]

```

returns what we both originally expected to see:

```
{{a, 1}, {a, 2}, {a, 3}}

```

Knowing how it actually works, I think that the function might be working as designed. But, I think they could have done a better job explaining it in the documentation. And of course, some additional examples that highlights how overlap works would help.

Good luck.

---

<div class="post-metadata">

**Author:** ![fgutiers2002](https://avatars.discourse-cdn.com/v4/letter/f/bb73d2/32.png) [@fgutiers2002](https://community.wolfram.com/u/fgutiers2002)\
**Post date:** [May 31, 2020, 10:56pm UTC](https://community.wolfram.com/t/a-doubt-about-subsetcases/16119/3 "2020-05-31T22:56:10Z")

</div>

Many thanks for this, Mike, now I fully understood.  
All the best,  
Fg

---

<div class="post-metadata">

**Author:** ![mikebesso](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mikebesso/32/22282_2.png) [@mikebesso](https://community.wolfram.com/u/mikebesso)\
**Post date:** [May 31, 2020, 11:48pm UTC](https://community.wolfram.com/t/a-doubt-about-subsetcases/16119/4 "2020-05-31T23:48:13Z")

</div>

Francisco:

You are welcome. Happy to help.

---

<div class="post-metadata">

**Author:** ![tonis](https://avatars.discourse-cdn.com/v4/letter/t/919ad9/32.png) [@tonis](https://community.wolfram.com/u/tonis)\
**Post date:** [June 1, 2020, 9:13am UTC](https://community.wolfram.com/t/a-doubt-about-subsetcases/16119/5 "2020-06-01T09:13:03Z")

</div>

Hello. I’m the developer of SubsetCases and related functions.

SubsetCases considers elements which appear multiple times as distinct, just like Subsets does:

```
In[1]:= Subsets[{a, a, b}, {2}]

Out[1]= {{a, a}, {a, b}, {a, b}}

```

To construct the solution of the given example, one can first generate all the subsets, then filter by an orderless version of the pattern:

```
In[2]:= Cases[Subsets[{1, 2, 3, a, a, b, c}, {2}], {OrderlessPatternSequence[a, _Integer]}]

Out[2]= {{1, a}, {1, a}, {2, a}, {2, a}, {3, a}, {3, a}}

```

Finally, to satisfy the Overlaps → False option value (the default for SubsetCases), one has to go through the list and drop each subset which contains any element already present in a previous subset.

One can see this more easily when looking at the result of SubsetPosition:

```
In[3]:= SubsetPosition[{1, 2, 3, a, a, b, c}, {a, _Integer}, Overlaps -> False]

Out[3]= {{4, 1}, {5, 2}}

```

Here one can see that no position element (integer) appears twice.

---

<div class="post-metadata">

**Author:** ![mikebesso](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mikebesso/32/22282_2.png) [@mikebesso](https://community.wolfram.com/u/mikebesso)\
**Post date:** [June 1, 2020, 1:29pm UTC](https://community.wolfram.com/t/a-doubt-about-subsetcases/16119/6 "2020-06-01T13:29:10Z")

</div>

Toni:

Thanks for the clarification.

Is there any way to get this explanation and a few examples that illustrate how overlaps works into the documentation?

---

<div class="post-metadata">

**Author:** ![fgutiers2002](https://avatars.discourse-cdn.com/v4/letter/f/bb73d2/32.png) [@fgutiers2002](https://community.wolfram.com/u/fgutiers2002)\
**Post date:** [June 3, 2020, 4:22am UTC](https://community.wolfram.com/t/a-doubt-about-subsetcases/16119/7 "2020-06-03T04:22:45Z")

</div>

Many thanks Toni. I would suggest putting something of this unto the documentation, maybe with a couple of additional examples to prevent possible ambiguities.

---

<div class="post-metadata">

**Author:** ![tonis](https://avatars.discourse-cdn.com/v4/letter/t/919ad9/32.png) [@tonis](https://community.wolfram.com/u/tonis)\
**Post date:** [June 3, 2020, 11:29am UTC](https://community.wolfram.com/t/a-doubt-about-subsetcases/16119/8 "2020-06-03T11:29:29Z")

</div>

Thanks for the question, we’ll improve the documentation.

---

<div class="post-metadata">

**Author:** ![343guiltyspark](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/343guiltyspark/32/35548_2.png) [@343guiltyspark](https://community.wolfram.com/u/343guiltyspark)\
**Post date:** [June 3, 2020, 1:17pm UTC](https://community.wolfram.com/t/a-doubt-about-subsetcases/16119/9 "2020-06-03T13:17:02Z")

</div>

I was having the same problem. This thread has been very helpful.
