Print all unique elements in the array/list
Здравейте, днес попаднах на следната задача:
Print all unique elements in an array.
input:
1 2 3 4 5 5 5 6
output:
1 2 3 4 6
Запознах се с Distinct(), но с него принтирам само това "1 2 3 4 5 6". Има ли някакъв друг подобен метод, с които да кажа, че искам числата да се запишат в нов лист, но само ако не се повтарят, потретват и пр. в първоначалния лист? И разбира се кода да е разбираем за човек, който е на ниво Fundamental, защото всички решения, които прегледах досега в stackoverflow бяха напълно неразбираеми. :)
Все пак мисля, че успях да измисля някакво решение само с Destinct плюс още един допълнителен for цикъл. Понеже нямам Judge за тази задача не съм сигурна дали не би се счупила програмата с някой инпут. И вече установих, че при инпут 1 1 2 2 6, принтирам 2 6, вместо само 6.
using System;
using System.Collections.Generic;
using System.Linq;
namespace W3resourceArrayExe
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.Write("Input the number of elements to be stored in the first array: ");
            int input = int.Parse(Console.ReadLine());
            Console.WriteLine($"Input {input} elements in the array:");
            var firstList = new List<string>();
            for (int i = 0; i < input; i++)
            {
                Console.Write($"element-{i} : ");
                string num = Console.ReadLine();
                firstList.Add(num);
             }
            var newListDestinct = firstList.Distinct().ToList();
            for (int i = 0; i < newListDestinct.Count; i++)
            {
                int counter = -1;
                for (int k = 0; k < firstList.Count; k++)
                {
                    if (newListDestinct[i] == firstList[k])
                    {
                        counter++;
                    }
                    if (counter > 0)
                    {
                        newListDestinct.RemoveAt(i);
                        break;
                    }
                }
            }
            Console.WriteLine(string.Join(" ",newListDestinct));
        }
    }
}
 
Hi again Axiomatik and thanks for your comment!
I see such complex Where() queries definitely are not for somebody from Fundamental module, but this information will be very usefull for me in the future.
Few hours ago I was trying to solved this exercise only with loops and maybe now it's working correctly, althout I'am not very sure but I have tested with 15 different inputs.
And thanks for your positive feedback about Array Manipulator. Maybe it will be easier for samebody to correct their code. :)
Best regards!
Elena
using System;
using System.Collections.Generic;
using System.Linq;
namespace W3resourceArrayExe
{
class MainClass
{
public static void Main(string[] args)
{
var listCount = int.Parse(Console.ReadLine());
var firstList = new List<int>();
for (int i = 0; i < listCount; i++)
{
int num = int.Parse(Console.ReadLine());
firstList.Add(num);
}
Console.WriteLine(string.Join(" ",firstList));
var newListDestinct = firstList.Distinct().ToList();
Console.WriteLine( string.Join(" ", newListDestinct));
int counter = 0;
for (int i = 0; i < newListDestinct.Count; i++)
{
for (int k = 0; k < firstList.Count; k++)
{
if (newListDestinct[i] == firstList[k])
{
counter++;
}
if (counter == 2)
{
newListDestinct.Remove(newListDestinct[i]);
i = -1;
counter = 0;
break;
}
}
counter = 0;
}
if (newListDestinct.Count == 0)
{
Console.WriteLine("There are no unique elements!");
}
else
{
Console.WriteLine(string.Join(" ", newListDestinct));
}
}
}
}