How to join two vectors in another that does not have repeated values?

0

I need to make a program that reads the values of two vectors R and S , and store its values in a third vector called V . The detail is that it can not contain any element repeated in the V vector.

I was able to make the program show the% integer% vector resulting from the union of V and R , but I still could not figure out how to not put repeated elements in S .

Here's the algorithm I've done:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercício6Testes
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] R = new int[10];
            int[] S = new int[10];
            int[] V = new int[20];


            Console.WriteLine("Este programa lê dois vetores e mostra a união dos dois sem valores repetidos");
            Console.WriteLine("Digite os 10 valores do vetor R: ");
            Console.WriteLine("\n");



            for (int i = 0; i < 10; i++)
            {

                Console.Write("Digite o valor de  R{0}: ", i + 1);   //LÊ O 1° VETOR
                R[i] = int.Parse(Console.ReadLine());

            }




            Console.Clear();
            Console.WriteLine("Agora digite os valores do vetor S: ");

            for (int j = 0; j < 10; j++)
            {
                Console.Write("Digite o valor de S{0}: ", j + 1); //LÊ O SEGUNDO VETOR
                S[j] = int.Parse(Console.ReadLine());


            }


            for (int k = 0; k < 10; k++)
            {
                V[k] = R[k];     //AQUI COLOCA TODOS OS VALORES DO VETOR R NAS
                                 //10 PRIMEIRAS POSIÇÕES DO VETOR V
            }



            int kw = 10;
            for (int L = 0; L < 10; L++)
            {
                V[kw] = S[L];  //AQUI COLOCA TODOS OS VALORES DO VETOR S NA
                kw++;          //SEGUNDA METADO DO VETOR V
            }



            Console.Clear();
            Console.WriteLine("União dos dois vetores: ");
            for(int mostra = 0; mostra < 20; mostra++)
            {
                Console.Write(V[mostra] + "\t");      //AQUI MOSTRA A UNIÃO DOS DOIS VETORES R e S DENTRO
                                                      //DO VETOR V.
                                                      //MOSTRA TUDO, ATÉ OS REPETIDOS!+......
            }




            Console.ReadKey();

        }
    }
}
    
asked by anonymous 07.09.2018 / 16:25

1 answer

2

There are several ways to do this and more easily, but following the algorithm requested, doing it smarter and simpler, without changing too much would be this:

using static System.Console;

namespace Exercício6Testes {
    public class Program {
        public static void Main() {
            int[] r = new int[10];
            int[] s = new int[10];
            int[] v = new int[20];
            WriteLine("Este programa lê dois vetores e mostra a união dos dois sem valores repetidos");
            WriteLine("Digite os 10 valores do vetor R:\n");
            var j = 0;
            for (var i = 0; i < 10; i++) {
                Write($"Digite o valor de  R{i + 1}: ");
                if (!int.TryParse(ReadLine(), out r[i])) {
                    i--;
                    continue;
                }
                if (!EhRepetido(v, r[i], j)) v[j++] = r[i];
            }
            WriteLine("Agora digite os valores do vetor S: ");
            for (var i = 0; i < 10; i++) {
                Write($"Digite o valor de S{i + 1}: ");
                if (!int.TryParse(ReadLine(), out s[i])) {
                    i--;
                    continue;
                }
                if (!EhRepetido(v, s[i], j)) v[j++] = s[i]; 
            }
            WriteLine("União dos dois vetores: ");
            for (int i = 0; i < j; i++) Write($"{v[i]}\t");
        }
        public static bool EhRepetido(int[] vetor, int item, int posicao) {
            for (int i = 0; i < posicao; i++) if (vetor[i] == item) return true;
            return false;
        }
    }
}

See running on .NET Fiddle . And no Coding Ground . Also I placed GitHub for future reference .

I upgraded the code. I do not understand why people keep programming in C # as it was in 2002.

I did a typing validation, the way it was if the person typed something wrong would break the application, you could put an error message there or do something else to make it clearer that you need to type again. Decrements the counter because the count can only go forward with valid data, and it does not execute anything else below.

I did a routine to identify repetition (not the best performance, but that's how I was doing). In addition to being easier to avoid using flags , it avoids repetition. In fact even data entry could avoid repetition. And I've checked directly into data entry that is easier and faster.

In the algorithm posted it did nothing filtering. For repeated filtering, you have to scan the entire array to the current position to see if it has been repeated, if it has quit the search, and only if none is repeated is guaranteed not to be repeated.

Finally, I only have to print the number of items that have validity in V . Since you do not use R and S for nothing, nor should you have created those variables.

    
07.09.2018 / 17:40