# Smallest groups in an array

### Introduction

Let's observe the following array:

[1, 1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1]

A group consists of the same digits next to each other. In the above array, there are 5 different groups:

[1, 1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1]

1, 1, 1
2, 2
1, 1, 1, 1
2, 2, 2
1, 1, 1

The smallest group of these is [2, 2], so we output [2, 2].

Let's take another example:

[3, 3, 3, 4, 4, 4, 4, 5, 5, 4, 4, 3, 3, 4, 4]

3, 3, 3
4, 4, 4, 4
5, 5
4, 4
3, 3
4, 4

You can see that there are multiple groups with the same length. The smallest groups are:

[3, 3], [4, 4], [4, 4] and [5, 5].

So we just output [3, 3], [4, 4], [4, 4], [5, 5] in any reasonable format. You may output these in any order.

Given an array consisting of only positive integers, output the smallest group(s) from the array. You can assume that the array will contain at least 1 integer.

### Test cases

Input: [1, 1, 2, 2, 3, 3, 4]
Output: [4]

Input: [1]
Output: [1]

Input: [1, 1, 10, 10, 10, 100, 100]
Output: [1, 1], [100, 100]

This is , so the submission with the least amount of bytes wins!

• – Leaky Nun May 1 '16 at 15:54
• can the input be a string? – downrep_nation May 1 '16 at 19:07
• @downrep_nation Hmm, how would you want to do that then? If you can do it with multidigit integers, than it's fine. – Adnan May 1 '16 at 20:57
• ints are very limited by size and strings are not. thats why im asking – downrep_nation May 1 '16 at 20:58
• @downrep_nation Okay, so how do you want to provide the input then for the last test case? 11101010100100 doesn't seem correct for input :p. – Adnan May 1 '16 at 21:09

# Pyth, 1412 11

mM_MmhbrQ8

Test Suite

2 bytes thanks to Jakube! And 1 byte thanks to isaacg!

Unfortunately, run length decoding doesn't quite do what we want it to do, but it will work with a minor workaround, but that makes it slightly longer than the manual implementation:

mr]d9.mhbrQ8

Credit to Jakube for finding this out.

• Btw, rld works, but you have to provide a list of pairs: mr]d9.mhbrQ8 – Jakube May 1 '16 at 21:33
• More about run length decoding: Run length decoding expects a list of pairs, such as what run length encoding returns, not an individual pair. – isaacg May 2 '16 at 0:20
• .bmYN == mM_M – isaacg May 2 '16 at 0:20
• @isaacg Ah, right that makes sense, I guess I wasn't thinking through that enough. Also that map trick is neat, thanks! – FryAmTheEggman May 2 '16 at 0:44

# Mathematica, 24 bytes

MinimalBy[Length]@*Split

This is a composition of two functions that can be applied to a list. Split takes all groups of consecutive numbers, and MinimalBy[Length] selects those with minimal length.

• Damn, just fired up Mathematica to test this... +1 :) – Martin Ender May 1 '16 at 16:01
• Now I'm wondering if I haven't made this too trivial :/. – Adnan May 1 '16 at 16:02

import Data.Lists
argmins length.group

.+:
<empty-line>

Try it online!

## Explanation

The input is 1,1,10,10,10,100,100.

The first line matches groups with same terms:

M!\b(\d+)(,\1\b)*

The input becomes:

1,1
10,10,10
100,100

The following two lines prepend the number of commas to the line:

(,()|.)+
$#2:$&

The input becomes:

1:1,1
2:10,10,10
1:100,100

Then they are sorted by this line, which looks for the first number as index:

O#.+

The input becomes:

1:1,1
1:100,100
2:10,10,10

Then these two lines find the place where the length is different, and remove everything onwards:

s^(.*\b(.+:).*)¶(?!\2).+

Try it online!

←kLg

Try it online!

ŒgLÐṂ

Try it online!

# 05AB1E, 5 bytes

γé¬gù

Try it online!

Commented:

γ      # split into chunks of equal adjacent elements
é     # sort the chunks by length
¬    # get the shortest (first) chunk without popping the list
g   # take the length of that chunk
ù  # keep all chunks with this length

Try it with step-by-step output!

# Japt-g, 4 bytes

Outputs a 2D-array.

òÎüÊ

Try it

òÎüÊ     :Implicit input of array
ò        :Partition between elements where
Î       :  The sign of their difference is truthy (not 0)
ü      :Group and sort by
Ê     :  Length
:Implicit output of first element

function(x,r=rle(x),l=min(r$l))lapply(r$v[r$l==l],rep,l) Try it online! -2 bytes thanks to Dominic van Essen. Uses rle and rep to collapse and reconstruct the array. # K (ngn/k), 27 bytes {((&/#'t)=#:)#t:(&~=':x)_x} Try it online! • t:(&~=':x)_x to split input on indices where the values change, storing in variable t • ((&/#'t)=#:)#t to filter down to items of the minimum length # PowerShell for Windows, 88 bytes$h=@{}
$args|%{$i+=$p-ne$_;$h.+$i+=,$p=$_}
($g=@($h|% v*|sort c*))|? c*t -eq $g[0].count Try it online! The script uses the Powershell alias sort that should be sort-object with Linux. Less golfed:$hashTable=@{}
$args|foreach-object{$i += ($prev -ne$_)
$prev =$_
$hashTable.+$i += ,$prev }$groups=@($hashTable|foreach-object Values|sort-object Count)$groups|where-object count -eq \$groups[0].count