+ 5

Codegolf(just for fun)

Let's see who can display how many times a value appears in a list using the least code[cpp only] the list should contain 10 values of your own choosing.

23rd Nov 2016, 4:41 PM
Remmiq Sliv
Remmiq Sliv - avatar
5 Réponses
+ 4
#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> v = {1,2,1,6,9,6,1,3,2,1}; int main() { cout<<count(v.begin(),v.end(),1); return 0;}
23rd Nov 2016, 4:42 PM
Remmiq Sliv
Remmiq Sliv - avatar
+ 4
Yup You are right @YellowBunny consider yourself #1
24th Nov 2016, 7:10 AM
Remmiq Sliv
Remmiq Sliv - avatar
+ 3
@YellowBunny Well technically your code doesn't display how many times the value appears instead it displays the value each time it appears. Still pretty neat.
24th Nov 2016, 2:08 AM
Remmiq Sliv
Remmiq Sliv - avatar
+ 2
/* This program outputs the amout of times a certain element is in a list in unary. */ #include <iostream> int main(){ int x[]{1,2,3,4,1,2,3,1,2,1},i=0; for(;i<10;i++)x[i]!=1?:std::cout<<1; }
23rd Nov 2016, 6:06 PM
YellowBunny
+ 1
@Marco Linde If it is looking for 2 instead it outputs 111. That is 3 in unary and thereby the amout of 2s. Your task did not say to output in decimal :) I could also declare another integer, replace the cout command with an increment command and output the number at the end, but that would be one more line of code.
24th Nov 2016, 6:32 AM
YellowBunny