# 删除每N个N

41

## 例

``````[3,2,6,1,1,6,2,3,2,6,6,6,6,1,6,6,3,3,7,2]
``````

``````[3,2,6,    6,2,3,2,6,6,6,6,  6,6,3,3,7,2]
``````

``````[3,2,6,    6,  3,2,6,6,6,6,  6,6,3,3,7  ]
``````

``````[3,2,6,    6,  3,2,6,6,6,6,  6,6,  3,7  ]
``````

``````[3,2,6,    6,  3,2,6,6,6,    6,6,  3,7  ]
``````

``````[3,2,6,6,3,2,6,6,6,6,6,3,7]
``````

## 测试用例

``````[1] -> []
[2] -> [2]
[1,1,1] -> []
[2,2,2] -> [2,2]
[1,1,2,2,2,3,3,3,3] -> [2,2,3,3,3]
[1,2,3,1,2,3,1,2,3,1,2,3] -> [2,3,3,2,3]
[3,2,6,1,1,6,2,3,2,6,6,6,6,1,6,6,3,3,7,2] -> [3,2,6,6,3,2,6,6,6,6,6,3,7]
[5,4,5,4,3,5,4,5,4,5,4,3,5,4,5,3,3,3,4,5,4,5,4,5,4,3,3,3,5,4] -> [5,4,5,4,3,5,4,5,4,3,5,4,5,3,3,4,5,5,4,4,3,3,5,4]
[6,4,5,8,2,9,3,1,8,5,3,5,5,6,3,5,1,2,3,9,3,5,8,7,5,9,1,3,4,8,2,3,4,7,8,5,8,5,3,1] -> [6,4,5,8,2,9,3,8,5,3,5,5,6,3,9,3,5,8,7,5,9,4,8,2,3,4,7,8,5,8,5,3]
[4,4,9,12,9,4,4,4,12,9,12,9,12,4,12,4,4,9,4,9,12,4,12,4,4,12,4,4,9,12,9,12,9,4,9,12,4,9,12,9,12,9,4,9,12,12,4,4,12,4] -> [4,4,9,12,9,4,4,12,9,12,9,12,4,12,4,9,4,9,12,4,12,4,12,4,4,9,12,9,12,4,9,12,9,9,12,9,4,9,12,12,4,4,12]
[2,2,11,11,11,11,6,2,2,6,6,2,2,2,2,6,11,6,11,11,2,11,2,6,6,11,2,11,6,2,6,6,6,6,6,11,2,11,11,6,6,6,11,6,2,6,2,6,6,2,11,2,2,11,11,11,11,2,6,2] -> [2,11,11,11,11,6,2,6,6,2,2,6,11,6,11,11,2,11,6,11,2,11,6,6,6,6,6,2,11,11,6,6,6,11,6,2,6,6,11,2,11,11,11,11,2,6]
[20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102] -> [20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,21,22,100,101,102,20,22,100,101,102,20,21,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,21,22,100,101,102,20,21,22,100,101,102,20,22,100,101,102,20,21,22,100,101,102,20,21,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,21,22,101,102,20,21,22,100,102,20,21,22,100,101,20,21,22,100,101,102,20,21,22,100,101,102,20,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,22,100,101,102,20,21,100,101,102]
``````

## 排行榜

``````## Language Name, N bytes
``````

`N`您提交的文件大小在哪里。如果您提高了分数，则可以通过打败旧分数保持标题。例如：

``````## Ruby, <s>104</s> <s>101</s> 96 bytes
``````

``````## Perl, 43 + 2 (-p flag) = 45 bytes
``````

``````## [><>](http://esolangs.org/wiki/Fish), 121 bytes
``````

15

# 果冻，10 8 字节

``````=;\S€%af
``````

### 这个怎么运作

``````=;\S€%af    Main link. Input: A (list)

;\         Compute the cumulative concatenations of the elements of A, i.e., the
list of A's prefixes.
=           Compare the elements of the nth prefix with the nth element of A.
S€       Compute the sum of the resulting arrays.
%      Take the nth sum modulo the nth element of A.
a     Logical AND; replace the nth remainder with the nth element of A
if the remainder is non-zero.
f   Filter; remove the zeroes (which do not occur in A) from the result.
``````

quintopia '16

@quintopia Jelly从左到右，所以这是第一个。`=`比较整数。例如，`[3,2,1]=;\`比较`3`用的元件`[3]``2`用的那些`[3, 2]``1`与那些`[3, 2, 1]`，给予`[1, [0, 1], [0, 0, 1]]`

quintopia '16

34

## awk，10个字节

``````++a[\$1]%\$1
``````

### 说明

``````++a[\$1]%\$1{print \$0}
``````

19

# Pyth，18 15 14 10 9个字节

``````f%/aYTTTQ
``````

``````f%/aYTTTQ       Implicit: Q=input
lambda T:
Y              Variable: starts as empty list.
a T             Append T to Y. Mutates Y.
/   T           Number of elts of Y that equal T.
%     T         Modulo by T
f       Q       Filter that lambda over Q.
``````

9

## Python，57个字节

``lambda l:[n for i,n in enumerate(l)if l[:i+1].count(n)%n]``

8

# Perl 6，28个字节

``{\$_=\$;grep {++.{\$^n}%\$n},@_} # 28 bytes``
``````{
\$_=\$;        # put \$_ in a clean state
grep {
++.{\$^n}   # increment \$_{ \$n } and declare \$n as an argument
% \$n       # check if the count is not divisible by \$n
}, @_        # the input
}
``````

``````# give it a lexical name for ease of use
my &code = {...}

sub check ( @a, @b ){
say try { so all code(@a) »==« @b } // False
}

check [1], []; # True
check [2], [2]; # True
check [1,1,1], []; # True
check [2,2,2], [2,2]; # True
check [1,1,2,2,2,3,3,3,3], [2,2,3,3,3]; # True
check [1,2,3,1,2,3,1,2,3,1,2,3], [2,3,3,2,3]; # True
check [3,2,6,1,1,6,2,3,2,6,6,6,6,1,6,6,3,3,7,2], [3,2,6,6,3,2,6,6,6,6,6,3,7]; # True
``````

``````# have to change it to a pure number
# when checking \$_         V
my &code = {\$_=\$;grep {++.{+\$^n}%\$n},@_}
# only matters here because we are using
# a subclass of Int but want it to use
# the same key as a normal Int

sub F ( Int \$v ) { IntStr.new: \$v, "Fail(\$v)" }
# prove that it passes through unchanged
say [F(2)];
# (Fail(2))

say code [3,2,6,F(1),F(1),6,F(2),3,2,6,6,6,F(6),F(1),6,6,F(3),3,7,F(2)];
# (3 2 6 6 3 2 6 6 6 6 6 3 7)
``````

7

## 严重的是22 17字节

``````k╗,;`;;╜o;╗c%`M@░
``````

``````6bbb2c3b603b3bbd6f3bbb6325604d40b0
``````

``````k╗                                Put empty list in reg0
,;                              Two copies of input
`        `M                   Map over the list
;;                           Make 2 extra copies of n
╜o                         Load reg0 and push the n onto it
;╗                       Put a copy back in reg0
c                      Count the number of copies of n in the list
%                     Take the result modulo n
@░                 Filter the original list with the resulting list
``````

10

Nico

6

## JavaScript ES6，34个字节

``````a=>a.filter(v=>f[v]=-~f[v]%v,f=[])
``````

edc65 '16

5

## Mathematica，40 38 36字节

``````Select[(f@#=#)&/@#,++f[#]~Mod~#>0&]&
``````

### 说明

``````g[list_] := (
Map[
(f[#] = #) &,
list
];
Select[
list,
Mod[++f[#], #] > 0 &
]
)
``````

（通常，我们可以简单地使用诸如`f[_] = 0`回退定义之类的方法来避免使用`Map`，但是我们不知道我们的函数是否曾经使用过，否则会留下一些先前定义的值，这会干扰我们的计数。）

5

## CJam，17个字节

``````Lq~{:X+_Xe=X%},p;
``````

### 说明

``````L                     Push empty array (let's call it L)
q~                   Push input and evaluate

{         },       Filter the array elements by...
:X                   Save number to variable X
+                  Append to L

_                 Duplicate
Xe=              Count occurences of X
X%            Take modulo X

The top element is popped to determine whether or not to keep that
element, with the updated L kept on the stack for the next iteration

p      Print the resulting filtered array
;     Pop L, which is now equal to the input array
``````

4

# JavaScript ES6，55个字节

``a=>a.filter((v,i)=>a.filter((w,j)=>j<=i&v==w).length%v)``

### 说明

``````a=>                            //a -> input array
a.filter(                     //filter array. only keep elements if inside function returns truthy
(v,i)=>                  //inside function to decide whether to keep items. v -> item; i -> index
a.filter((w,j)=>    //get all ocurrences of v that occur before index i
j<=i&v==w      //(this is done by keeping all items w at index j, if j <= i and v == w
).length%v          //get length (count ocurrences), and mod v.
//this will only be falsy if the number of ocurrences of v up to this index is divisible by v. (0 -> falsy, positive -> truthy)
)                             //so, we don't keep every second 2, every third 3, etc.``````

3

# J，18个字节

``````#~((0<]|+/@:=){:)\
``````

``````   (#~((0<]|+/@:=){:)\) 1 2 3 1 2 3 1 2 3 1 2 3
2 3 3 2 3
``````

2

## PowerShell，56个字节

``````param(\$a)\$b=,0*(\$a|sort)[-1];\$a|%{if(++\$b[\$_-1]%\$_){\$_}}
``````

（快速插入我的“展示您的语言” 答案，我将在此进行详细说明）

``````PS C:\Tools\Scripts\golfing> \$testc = .\remove-every-nth-n.ps1 @(2,2,2)

PS C:\Tools\Scripts\golfing> \$testc
2
2

PS C:\Tools\Scripts\golfing> \$testc.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
``````

1

# R，110 98 99 92字节

``````function(a){for(i in 1:max(a))for(j in seq_along(b<-which(a==i)))if(j%%i<1)a[b[j]]=0;a[a>0]}
``````

1
92个字节：`function(a){for(i in 1:max(a))for(j in seq_along(b<-which(a==i)))if(j%%i<1)a[b[j]]=0;a[a>0]}`
Alex A.

1

# MATL，20字节

``````tu"t@=f@@h0h)0w(]tg)
``````

### 说明

``````tu        % implicitly input array. Duplicate and get unique elements
"         % for each unique element, say "i"
t       % duplicate
@=f     % indices of occurrences of i
@@h0h   % build index representing i-th occurrences (Matlab's i:i:end)
)       % keep only indices of i-th occurrences
0w(     % set those entries to 0
]         % end for loop
tg)       % keep nonzeros only. Implicit display
``````

1

# R，63个字节

``````f=function(z){n=seq(z);for(i in n)z[which(z==i)[n*i]]=0;z[z>0]}
``````

1

## C＃，224个字节

``````List<int>R(List<int>l,int n=1){l=l.Where(w=>w!=0&&w!=1).ToList();for(int i=0,t=0;i<l.Count;i++){if(l[i]==n&&++t==n){l[i]=0;t=0;}}return !l.Any()||n>l.Max()?l:R(l,++n);}
``````

``````List <int> R(List <int> l, int n = 1)
{
l = l.Where(w => w > 1).ToList();
for (int i = 0, t = 0; i < l.Count; i++)
{
if (l[i] == n && ++t == n)
{
l[i] = 0;
t = 0;
}
}
return !l.Any() || n > l.Max() ? l : R(l, ++n);
}
``````

@ peter-taylor，您说得对，谢谢。此外，还必须添加一些代码来修复错误。

@ peter-taylor谢谢，我也替换`w != 0 && w !=1``w > 1`

0

# C＃-177字节

``````void r(List<int> i){for(int c=1,k=1,m=i.Max();k<=m;k++){var n=new List<int>();foreach(var o in i)if(o==k&&c++==k)c = 1;else n.Add(o);i=n;}Console.WriteLine(string.Join(" ",i));}
``````

``````void r(List<int> i)
{
for (int c = 1, k = 1, m = i.Max(); k <= m; k++)
{
var n = new List<int>();
foreach (var o in i)
if (o == k && c++ == k)
c = 1;
else
i = n;
}
Console.WriteLine(string.Join(" ", i));
}
``````

4

LegionMammal978 '16

0

## Mathematica，63个字节

``````Fold[Delete[#,Position[#,#2][[#2;;;;#2]]~Check~{}]&,#,Union@#]&
``````

0

# Ruby，120个字节

``````->a{1.upto(a.max).each{|i|k=0;a.length.times.each{|j|k+=1if a[j]==i;a[j]=''if k%i==0&&a[j]==i;a[j]}};a.select{|i|i!=''}}
``````

0

## TI-BASIC，47个字节

``````Input X
For(I,1,dim(∟X
∟X(I
If fPart(sum(∟X=Ans),1,I)/Ans
Ans→L₁(1+dim(L₁
End
L₁
``````

0

## APL，16个字符

``````{⍵/⍨×⍵|+/¨⍵=,\⍵}
``````

• `,\⍵`：向量的向量前缀，直到参数的第n个元素
• `+/¨⍵=`：每个前缀向量，计算等于第n个元素本身的个数
• `×⍵|`：mod的符号（即：如果除法的余数为0，则为0，否则为1）
• `⍵/⍨`：仅保留mod为0的元素

0

## 球拍179字节

``````(λ(l)(define m(apply max l))(let g((n 1)(c 0))(set! l(for/list((i l))(if(= i n)(begin
(set! c(+ 1 c))(if(= 0(modulo c n))0 i))i)))(if(< n m)(g(+ 1 n)0)(filter(λ(x)(> x 0))l))))
``````

``````(define f
(λ(l)
(define m (apply max l))
(let loop ((n 1) (c 0))
(set! l (for/list ((i l))
(if (= i n)
(begin
(set! c (+ 1 c))
(if (= 0 (modulo c n))
0 i ))                  ; replace by 0
i )))
(if (< n m)
(loop (+ 1 n) 0)
(filter (λ(x)(> x 0)) l)                ; remove all 0s
))))
``````

``````(f '[1])
(f '[2])
(f '[1 1 1])
(f '[2 2 2])
(f '[1 1 2 2 2 3 3 3 3])
(f '[1 2 3 1 2 3 1 2 3 1 2 3])
(f '[3 2 6 1 1 6 2 3 2 6 6 6 6 1 6 6 3 3 7 2])
``````

``````'()
'(2)
'()
'(2 2)
'(2 2 3 3 3)
'(2 3 3 2 3)
'(3 2 6 6 3 2 6 6 6 6 6 3 7)
``````