CHOLAT - Editorial

PROBLEM LINK:

Practice
Contest

Author: Deepak Chaudhary
Tester: Vikas Yadav
Editorialist: Vikas Yadav

DIFFICULTY:

EASY

PREREQUISITES:

Array
Set

PROBLEM:

Count the distinct elements in the given array.

QUICK EXPLANATION:

Create a set using the elements of the given array and print the size of this set.

EXPLANATION:

A set is a well-defined collection of distinct elements so its size will give the result.
If don’t know about set, just count all the distinct elements of the given array.

SOLUTIONS:

Setter's Solution
// Chef and Chocolate

#include <bits/stdc++.h>
using namespace std;
#define int long long

void solve()
{
   int test_cases;
   cin >> test_cases;
   
   while(test_cases--)
   {
       int n;
       cin >> n;

       int arr[n];
       for(int i = 0; i < n; i++)
           cin >> arr[i];
       
       unordered_map<int,int> mp;
       
       for(int i = 0; i < n; i++)
       {
          mp[arr[i]]++;
       }

       cout << mp.size() <<"\n";
   }
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    solve();

    return 0;
}

Tester's Solution
// Chef and Chocolate

#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
	int test;
	cin >> test;
	while(test--)
	{
		ll n;
		cin>>n;
		ll arr[n];

		set<ll> s;

		for(ll i = 0; i < n; i++)
		{
			cin>>arr[i];
			s.insert(arr[i]);
		}

		cout<<(s.size())<<"\n";	
	}
}

Solution in C without using set
// Chef and Chocolate
// without using set

#include <stdio.h>

int main()
{
    int test_cases;
    scanf("%d", &test_cases);

    while(test_cases--)
    {
        int n;
        scanf("%d", &n);

        int arr[n], count = 0;
        
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &arr[i]);
        }
        
        for(int i = 0; i < n; i++)
        {
            int j = 0; 
            for (j = 0; j < i; j++)
            {
                if (arr[i] == arr[j]) 
                break; 
            }

            if (i == j) 
                count++; 
        }

        printf("%d\n", count);  
    }
	
	return 0;
}