287,387 views
17 votes
17 votes
write a method named numunique that accepts three integers as parameters and that returns the number of unique integers among the three. for example, the call numunique(18, 3, 4) should return 3 because the parameters have 3 different values. by contrast, the call numunique(6, 7, 6) would return 2 because there are only 2 unique numbers among the three parameters: 6 and 7. compare your solution to your neighbors'. did you all solve it the same way?

User Nikhil Kuriakose
by
2.8k points

1 Answer

11 votes
11 votes

#include <iostream>

int numunique(int idx, int idy, int idz) {

return (idx==idy && idx==idz && idy==idz) ? 1 : (idx!=idy && idx!=idz && idy!=idz) ? 3 : 2;

}

int main(int argc, char* argv[]) {

std::cout << "Enter 3 numbers: ";

int x,y,z; std::cin>>x>>y>>z;

std::cout << "There is/are " << numunique(x,y,z) << " unique number(s).\\";

return 0;

}

User Dracontis
by
3.3k points