1

im looking for nice and clean way to see if there are three equal numbers in an array.

Right now i have this:

for (int i = 0; i < nr ; i++)
{
    if(a[i] == 1){one++;}
    else if(a[i] == 2){two++;}
    else if(a[i] == 3){three++;}
    else if(a[i] == 4){four++;}
    else if(a[i] == 5){five++;}
    else if(a[i] == 6){six++;}
}

if(one >= 3){
    printf("Tre tal finns i ettor, 3p\n");
}else if(two >= 3){
    printf("Tre tal finns i tvår, 6p\n");
}else if(three >= 3){
    printf("Tre tal finns i treor, 9p\n");
}else if(four >= 3){
    printf("Tre tal finns i fyror, 12p\n");
}else if(five >= 3){
    printf("Tre tal finns i femmor, 15p\n");
}else if(six >= 3){
    printf("Tre tal finns i sexor, 18p\n");
}

Where a (integer) is an array of 5 elements(containing elemets 1-6) and "nr" is an variable to keep track for the arrays length.

If anyone got a nicer and better way to do this, please reply.

uzr
  • 1,210
  • 1
  • 13
  • 26

4 Answers4

6

Generalize it for a histogram, and basically do the first step of counting sort:

int histogram[n]; //variable length array are fine in c99, if using older c - malloc
for (int i = 0; i < n; i++) histogram[i] = 0; //init
for (i = 0; i < nr; i++)
   histogram[a[i]]++;
for (i = 0; i < n; i++)
   if (histogram[i] >= 3) //found it
//....    
amit
  • 175,853
  • 27
  • 231
  • 333
  • 1
    Don't use `new[]` in C, and don't assume that newly allocated heap memory is zeroed. – unwind Oct 15 '13 at 09:52
  • 1
    Thanks, didnt see the C tag. give me a sec to fix it – amit Oct 15 '13 at 09:53
  • Also, assumes you know the maximum possible value and that it's sensible. I mean you *could* try to allocate an array large enough to fit every possible `unsigned int` (or whatever underlying type you need) but that's a lot of memory. This isn't a bad idea, but there are better approaches. – Nik Bougalis Oct 15 '13 at 09:55
  • You can do `int histogram[n] = {0}` to fill it with zero. – Co_42 Oct 15 '13 at 09:57
  • Your post is correct answer to OP, keeping frequency count without array is bad design. + – Grijesh Chauhan Oct 15 '13 at 13:28
3

I would like to use switch-case as:

switch((a[i]){
 case 1: one++;
         break;
 case 2: two++;
         break;
 case 3: three++;
         break;
 case 4: four++;
         break;
 case 5: five++;
         break;
 case 6: six++;
         break;
 //default: if you want to add 
}
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
0

Have they to be next to each other? Then You would only need a flag var.

Indeed, if It doesn't care to You if You've more than a number repeated but just know them exist, it'll be better.

Btc Sources
  • 1,912
  • 2
  • 30
  • 58
0

If you want a truly general solution then sort your input array; once sorted, finding all instances where a number appears more than n times, for whatever n you want, becomes trivial.

If you want something for a more limited domain or a more targeted solution, then others have given you some good hints.

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37