Necesito ayuda con un program en c#

(1/1)

Emily:
hola me preguntaba si alguien podría ayudarme con un programa en c#
necesito ingresar 10 números en un vector y los números que se repiten los tengo que poner con 0
Tiene que imprimir esto:
Suponga los valores de entrada del arreglo:

10 4 9 11 4 7 10 30 11 10

El resultado sería:

10 4 9 11 0 7 0 30 0 0

Eleкtro:
Hola. ¿Cual es la razón que te ha llevado a publicar una pregunta sobre C#, en el foro de Visual Basic 6? :-\

Bueno, una forma de hacer lo que pides en caso de que quieras preservar los elementos del array original sin modificar, sería desarrollar una función genérica para iterarlo de la siguiente manera:

Código
/// ----------------------------------------------------------------------------------------------------
/// <summary>
/// Replaces the duplicated items in the source collection using the specified value.
/// </summary>
/// ----------------------------------------------------------------------------------------------------
/// <typeparam name="T">
/// The type of the items in te collection.
/// </typeparam>
///
/// <param name="collection">
/// The source collection.
/// </param>
///
/// <param name="value">
/// The value that will be used to replace duplicated items.
/// </param>
/// ----------------------------------------------------------------------------------------------------
/// <returns>
/// The resulting <see cref="IEnumerable&lt;T&gt;"/>.
/// </returns>
/// ----------------------------------------------------------------------------------------------------
public static IEnumerable<T> ReplaceDuplicates<T>(T[] collection, T value) {
 
   HashSet<T> hset = new HashSet<T>();
 
   foreach(T item in collection) {
       if (hset.Add(item)) {
           yield return item;
       } else {
           yield return value;
       }
   }
 
}

Modo de empleo:
Código
int[] arr = { 10, 4, 9, 11, 4, 7, 10, 30, 11, 10 };
int[] newArr = ReplaceDuplicates<int>(arr, 0).ToArray();
 
Console.WriteLine(string.Join(", ", newArr));

Resultado de ejecución:
Cita de: Visual Studio's Debug Window Output

10, 4, 9, 11, 0, 7, 0, 30, 0, 0

Hay varias formas de hacerlo. También puedes utilizar un for para reemplazar los elementos del array original, o recurrir al elegante pero no muy eficiente LINQ.

EDITO: Por ejemplo:
Código
int[] arr = { 10, 4, 9, 11, 4, 7, 10, 30, 11, 10 };
int[] newArr = arr.Select((a, b) => Array.IndexOf(arr, a) == b ? a : 0).ToArray();
 
Console.WriteLine(string.Join(", ", newArr));

¡Saludos!

okik:
Código
using System;
using System.Linq;
...
 



Citar

int[] MiArray = {10,4,9,11,4,7,10,30,11,10};
        
for (int Index = 0; Index <= MiArray.Length - 1; Index++) {
    //Cuenta el número  que se repite un elemento del array
    var CountItems = (from ItemRep in MiArray where (ItemRep == MiArray[Index]) select ItemRep).ToList().Count;
    if (CountItems > 1)
    {
        MiArray[MiArray.ToList().LastIndexOf(MiArray[Index])] = 0;
    }
}

Console.WriteLine(string.Join(", ", MiArray));
Console.ReadLine();



He rectificado el código anterior, ya me pareció extraño que resultara. En el caso de "10, 4, 4, 4, 4, 4, 4, ", no hubiera devuelto "10,4,0,0,0,0,0".

Código
           int[] MiArray = { 10, 4, 9, 11, 4, 7, 10, 30, 11, 10 };
 
 
           foreach (var Item in MiArray)
           {
               for (int Index = MiArray.ToList().IndexOf(Item) + 1; Index <= MiArray.Length - 1; Index++)
               {
                   if (MiArray[Index] == Item) { MiArray[Index] = 0; };
               }
           }
           Console.WriteLine(String.Join(",", MiArray));
           Console.ReadLine();

Código:

10, 4, 9, 11, 0, 7, 0, 30, 0, 0


Así también funcionaría:
Código
 int[] MiArray = { 10, 4, 9, 11, 4, 7, 10, 4, 4, 4 };
 
 
           for (int frtIndex = 0; frtIndex <= MiArray.Length - 1; frtIndex++)
           {
               for (int scdIndex = frtIndex + 1; scdIndex <= MiArray.Length - 1; scdIndex++)
               {
                   if (MiArray[scdIndex] == MiArray[frtIndex]) { MiArray[scdIndex] = 0; };
               }
           }
           Console.WriteLine(String.Join(",", MiArray));
           Console.ReadLine();
 
 

Código:

10, 4, 9, 11, 0, 7, 0, 0, 0, 0

Emily:
Muchisimas Gracias :D
Me a ayudado mucho

Navegación

[0] Índice de Mensajes