Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users

Dashboard
Notifications
Mark all as read
Challenges

Cumulative Counts

+8
−0

Challenge

Given an array of numbers return the cumulative count of each item.

This is the number of times an item has occurred so far.

Examples

[1,1,2,2,2,1,1,1,3,3] -> [1,2,1,2,3,3,4,5,1,2]
[3,7,5,4,9,2,3,2,6,6] -> [1,1,1,1,1,1,2,2,1,2]

Brownie points for beating my 7 byte APL answer.

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

10 answers

+3
−0

APL (Dyalog Unicode), 11 bytes (SBCS)

1 1∘⍉+\∘.=⍨

Try it online!

This was a fun APL exercise. Working on figuring out how to get it down to 7 bytes.

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

General comments (4 comments)
+3
−0

BQN, 5 bytes

1+⊒

Try it here!

3 characters, but, as there's no SBCS codepage for BQN, it must be counted as UTF-8.

Two of the three characters are just adding one to the built-in that almost solves the challenge too.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+3
−0

Jelly, 4 bytes

¹Ƥċ"

Try it online!

   "    For each element of the input,
  ċ     how many times does it occur in
¹Ƥ "    the corresponding prefix of the input?
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

Japt, 14 11 8 bytes

£¯YÄ è¥X

Try it

-3 bytes thanks to @Shaggy

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

General comments (2 comments)
+2
−0

APL (Dyalog Unicode), 11 7 bytes (SBCS)

Razetime and rak1507 came up with 7 byte equivalents of my original dfn (this one's rak1507's). See their solutions below.

+/¨⊢=,\

Try it online!

+/¨⊢=,\
     ,\ ⍝ Prefixes of the list
    =   ⍝ Compare every prefix
   ⊢   ⍝ to the corresponding element in the original list
+/     ⍝ Sum each to get a count of how many elements in each prefix match
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

General comments (3 comments)
+1
−0

JavaScript (Node.js), 34 bytes

(a,d={})=>a.map(v=>d[v]=1+d[v]||1)

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Scala 3, 50 44 bytes

? => ?.indices zip?map(?take _+1 count _.==)

Try it in Scastie!

This is an annoying, stupid approach. The more elegant one using inits was much longer (x=>x.inits.toSeq.reverse.tail zip x map(_ count _.==)).

? =>   //The input
?.indices //The indices
  zip ?   //Zip them with the elements of the input
  .map(   //For every index i and the corresponding element x,
    ? take _+1   //Take the first i+1 elements of the input
    count        //Count how many of them
    _.==)        //Are equal to the element x
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Jelly, 7 bytes

=þ`ÄŒDḢ

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Husk, 4 bytes

Sz#ḣ

Try it online!

Explanation

Sz#ḣ
Sz   zip the input
   ḣ with its prefixes
  #  using the count function
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+0
−0

Ruby, 36 bytes

->a{i=-1;a.map{a[0..i+=1].count _1}}

Try it online!

The code in the TIO link is 2 bytes longer because _n block parameter names are not supported on TIO's Ruby instance yet. It will work the same, however. The code is:

->a{i=-1;a.map{a[0..i+=1].count a[i]}}
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!