DISTINCT - Editorial

PROBLEM LINK:

Practice
Contest

Author: Deepanshu Kapoor
Tester: Chandan Boruah
Editorialist: Chandan Boruah

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Brute force.

PROBLEM:

Given a list of names, find the unique names. Print the count of unique names and print then in lexicographically ascending order.

Advanced EXPLANATION:

Store the names in a linked list. If its already there in the list, then don’t add the name to the list.

EXPLANATION:

At first, iterate over the names and add to a linked list the names. If the name is already there in the list from previous additions which can be checked by a condition, then don’t add the current name in the loop to the list. Sort the list. Print the count of names in the list and print the list in the sorted order.

SETTER’S SOLUTION:

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

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        set < string > val;
        for(int i=0;i<n;i++){
            string s;
            cin>>s;
            val.insert(s);
        }
        cout<<val.size()<<" ";
        for(set<string > ::iterator it= val.begin();it!=val.end();it++) cout<<*it<<" ";
        cout<<endl;
    }
    return 0;
}

TESTER’S SOLUTION:

using System;
using System.Collections.Generic;
class some
{
	public static void Main()
	{
		int t=int.Parse(Console.ReadLine());
		for(int test=0;test<t;test++)
		{
			int n=int.Parse(Console.ReadLine());
			List<string>ss=new List<string>();
			for(int i=0;i<n;i++)
			{
				string now=Console.ReadLine();
				if(!ss.Contains(now))
				ss.Add(now);
			}
			ss.Sort();
			Console.WriteLine(ss.Count);
			foreach(string tt in ss)Console.WriteLine(tt);
		}
	}
}