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
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

50%
+0 −0
Challenges Make a frequency table (histogram)

C (gcc), 45, 42 bytes t[9999];f(s,a)int*a;{for(;s;)t[a[--s]]++;} Try it online! Assumptions: Passing the array size to a function can be used as a means to deal with empty arrays (not s...

posted 2y ago by Lundin‭  ·  edited 2y ago by Lundin‭

Answer
#2: Post edited by user avatar Lundin‭ · 2022-06-30T09:55:55Z (over 2 years ago)
Recursion isn't always the best solution...
  • # [C (gcc)], 45 bytes
  • <!-- language-all: lang-c -->
  • t[9999];f(s,a)int*a;{s?t[*a++]++,f(s-1,a):0;}
  • [Try it online!][TIO-l50u1psr]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-l50u1psr]: https://tio.run/##bU/RasMwDHzPVxyBgl2r4Gxry@aNfUgIIyRx8UOaUrt9WPC3Z3LNRgc1Aks63Z3UbQ5dtyyhfuXXGCs8tdIdw7o1s/8M9bpVqlGKGNhUDL1pE5fr5Hqczjz2FSBSJYu5QG5ZUc4opeHaTmfBHbgPbeDeK50@pSRDgLMi1K6Rf6RVj1UfUZIj3JAkcboEL8qY9cZh9EMQgTR59z1MFoH7RSyK5DK27ni/jRXAnpA2qBs5o6IUT4Rnwpaw40BEdsm3iKSWifqOqBEfT1XVf3mW3P@a7G7B@ZbtXjh7oBKXHw "C (gcc) – Try It Online"
  • ---
  • Assumptions:
  • - Passing the array size to a function can be used as a means to deal with empty arrays (not supported in C).
  • - A table of integers can be regarded as a hashtable with an integer value as hash key and the value's frequency as data. Or if you will: a key-value pair.
  • - Maximum integer value 9999.
  • - The global variable `t` may have its contents printed outside the function.
  • # [C (gcc)], <del>45</del>, 42 bytes
  • <!-- language-all: lang-c -->
  • t[9999];f(s,a)int*a;{for(;s;)t[a[--s]]++;}
  • [Try it online!][TIO-l50unxgv]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-l50unxgv]: https://tio.run/##bU/LasMwELz7KwZDQKrXILdNQlH7JUIUY1tFB8chUnuo0be7q4iWFCIGtK@Z2R3aj2HYtmhe@FntRKBe@lN86PXqlovQQctoetO2wdqm0Wn7WvyI84Vn3iNEzmS1ViglJ@oVtdScZzZX4N@Uhn/tVP6aRnIL8E5E4638I@1G7MaEmjzh2skS588YRJ2K3jzNYYoikqLgv6fFIXK9SlWVXeben263cQI4EvIGxsoVHWU8Ep4Ie8KBgYTiUm4RWa0Q1Q1RId2f6rr/8ix5/DU5XMHxnu2eObqjkrYf "C (gcc) – Try It Online"
  • ---
  • Assumptions:
  • - Passing the array size to a function can be used as a means to deal with empty arrays (not supported in C).
  • - A table of integers can be regarded as a hashtable with an integer value as hash key and the value's frequency as data. Or if you will: a key-value pair.
  • - Maximum integer value 9999.
  • - The global variable `t` may have its contents printed outside the function.
#1: Initial revision by user avatar Lundin‭ · 2022-06-30T09:47:32Z (over 2 years ago)
# [C (gcc)], 45 bytes

<!-- language-all: lang-c -->

    t[9999];f(s,a)int*a;{s?t[*a++]++,f(s-1,a):0;}

[Try it online!][TIO-l50u1psr]

[C (gcc)]: https://gcc.gnu.org/
[TIO-l50u1psr]: https://tio.run/##bU/RasMwDHzPVxyBgl2r4Gxry@aNfUgIIyRx8UOaUrt9WPC3Z3LNRgc1Aks63Z3UbQ5dtyyhfuXXGCs8tdIdw7o1s/8M9bpVqlGKGNhUDL1pE5fr5Hqczjz2FSBSJYu5QG5ZUc4opeHaTmfBHbgPbeDeK50@pSRDgLMi1K6Rf6RVj1UfUZIj3JAkcboEL8qY9cZh9EMQgTR59z1MFoH7RSyK5DK27ni/jRXAnpA2qBs5o6IUT4Rnwpaw40BEdsm3iKSWifqOqBEfT1XVf3mW3P@a7G7B@ZbtXjh7oBKXHw "C (gcc) – Try It Online"

---

Assumptions:
- Passing the array size to a function can be used as a means to deal with empty arrays (not supported in C).
- A table of integers can be regarded as a hashtable with an integer value as hash key and the value's frequency as data. Or if you will: a key-value pair.
- Maximum integer value 9999.
- The global variable `t` may have its contents printed outside the function.