# 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


¹         $(grammar stuffs)  # JavaScript (ES6), 106 a=>(a.map((v,i)=>v==a[i-1]?g.push(v):h.push(g=[v]),h=[]),h.filter(x=>!x[Math.min(...h.map(x=>x.length))]))  Test f=a=>(a.map((v,i)=>v==a[i-1]?g.push(v):h.push(g=[v]),h=[]),h.filter(x=>!x[Math.min(...h.map(x=>x.length))])) console.log=x=>O.textContent+=x+'\n' ;[[1, 1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1] , [3, 3, 3, 4, 4, 4, 4, 5, 5, 4, 4, 3, 3, 4, 4] , [1, 1, 2, 2, 3, 3, 4] , [1] , [1, 1, 10, 10, 10, 100, 100]] .forEach(t=>console.log(t+' -> '+f(t).join )) <pre id=O></pre> • Does h.map(length) not work? – Leaky Nun May 2 '16 at 9:59 • @KennyLau no, for it to work length should be a function with the string as argument, not a method of string – edc65 May 2 '16 at 10:02 • @edc65 Actually, a property of String. Not a method. – Not that Charles May 2 '16 at 18:58 ## JavaScript (ES6), 113 bytes a=>a.map(n=>n==c[0]?c.push(n):b.push(c=[n]),c=b=[])&&b.sort((a,b)=>a[l]-b[l],l='length').filter(e=>e[l]==b[0][l])  # Retina, 91858079777675 74 bytes M!\b(\d+)(,\1\b)* (,()|.)+$#2:$& O#.+ s^(.*\b(.+:).*)¶(?!\2).+$1
.+:
<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).+
$1  The input becomes: 1:1,1 1:100,100  Then the numbers are removed by these two lines: .+: <empty-line>  Where the input becomes: 1,1 100,100  • @Adnan Thanks, fixed. – Leaky Nun May 2 '16 at 9:35 ## APL, 25 chars {z/⍨(⊢=⌊/)≢¨z←(1,2≠/⍵)⊂⍵}  In English: • put in z the argument split where a number is different than the one preceding; • compute the length of each subarray • compare the minimum with each of the lengths producing a boolean... • ... that is used to reduce z • Commute. Commute. Commute! ⍵⊂⍨1,2≠/⍵ – Zacharý Jul 31 '17 at 22:38 # J, 31 bytes [:(#~[:(=<./)#@>)]<;.1~1,2~:/\]  Input is an array of values. Output is an array of boxed arrays. ## Usage  f =: [:(#~[:(=<./)#@>)]<;.1~1,2~:/\] f 1 1 2 2 3 3 4 ┌─┐ │4│ └─┘ f 3 3 3 4 4 4 4 5 5 4 4 3 3 4 4 ┌───┬───┬───┬───┐ │5 5│4 4│3 3│4 4│ └───┴───┴───┴───┘  ## Explanation [:(#~[:(=<./)#@>)]<;.1~1,2~:/\] Input: s ] Identity function, get s 2 The constant 2 \ Operate on each overlapping sublist of size 2 ~:/ Check if each pair is unequal, 1 if true else 0 1, Prepend a 1 to that list ] Identity function, get s <;.1~ Using the list above, chop s at each true index [:( ) Operate on the sublists #@> Get the length of each sublist [:( ) Operate on the length of each sublist <./ Get the minimum length = Mark each index as 1 if equal to the min length else 0 #~ Copy only the sublists with min length and return  ## Clojure, 65 bytes #(let[G(group-by count(partition-by + %))](G(apply min(keys G))))  Uses + as identity function as (+ 5) is 5 :) The rest should be obvious, G is a hash-map used as a function and given a key it returns the corresponding value. # Brachylog, 6 bytes ḅlᵒlᵍh  Try it online! Input through the input variable and output through the output variable. ḅ The list of runs of consecutive equal elements of the input lᵒ sorted by length lᵍ and grouped by length has the output variable h as its first element.  Although, unlike ḅ, ᵍ groups non-consecutive equal elements, the lᵒ is still necessary to find the group with the shortest lengths, and it works because the order of groups in the output from ᵍ is determined by the position of the first element of each group, so that ᵍhᵐ could function as sort of a deduplicate by pseudo-metapredicate. # Perl 5-MList::Util=pairkeys,min -a, 69 bytes map$r{y/ //}.="[ $_]",pairkeys"@F "=~/((\d+ )\2*)/g;say$r{min keys%r}


Try it online!