Start New Discussion within our Software Development Community

Write a complete program that reads three integers from the console and outputs the number of distinct values entered. For example if the input is 34 55 23 then the output will be 3, since there are three distinct values . If the input is 42 78 42 then the output will 2 since there are two distinct values . Finally, if the input is 67 67 67 then the output will be 1 since there is just one distinct value .

Here is my source code:

#include<iostream>
using namespace std;

int main()
{
    int a, b, c, distinct;

    cin >> a >> b >> c;
    if(a != b && b != c && a != c)
    {
        distinct++;
        cout << distinct << endl;
    }
    else if((a != b && b != c && a == c) 
    || (a == b && b == c && a != c) 
    || (a != b && b == c && a != c) || (a == b && b != c && a != c))
    {
        distinct++;
        cout << distinct << endl;
    }
    else
    {
        cout << distinct << endl;
    }

    return 0;
}

Here is my input:
34 55 23

Here is my output:
1

Here is the expected output I want to get:
3

Is there anyway I can fix this?

Here is an example of how this can be achieved using std library.

#include <iostream>
#include <algorithm>
#include <vector>

int main() 
{
    std::vector<int> v{1,2,3,1,2,3,3,4,5,4,5,6,7};
    std::sort(v.begin(), v.end()); 
    auto last = std::unique(v.begin(), v.end());
    v.erase(last, v.end());
    for (const auto& i : v)
      std::cout << i << " ";
    std::cout << "\n";
}

You would of course have to get your input into a vector container and modify the output.

This question has already been answered. Start a new discussion instead.