___ ___ verify erkimt repeated within the array number C # ______ qstntxt ___

I've created a pre-defined array with some numbers, and I want to know how many repeated numbers it has in the array. but it is only returning the size of the array and not qtd of repeated numbers. and also wanted to remove the repeated numbers.

%pre%     
______ azszpr271810 ___

Assign your array inside a HashSet, as it does not accept repeated values, and then you compare the sizes of both and know how many values were repeated:

%pre%     
______ azszpr271881 ___

As commented,

  

Manieiro :   Set "how many numbers repeated", this can be interpreted as   various forms.

The proposed answer presents a solution that depends exactly on which question is being asked.

  

Gabriel Coletta

%pre%

The result is 5 , a result of the original size difference of %code% subtracted from its %code% , which can also be written as %code%

%pre%

However, we come to a subjective point, 5 actually represents the number of repetitions of the values after their first occurrences, in other words, 5 is equal amount of duplicates or recurrences.

But when we look at the original set %code% , we can see that it has 8 elements of repeated values (which have more than one occurrence). What can be seen more easily by rearranging and removing unique occurrences %code% . And this difference greatly impacts the purpose of the query and what you intend to do with that result.

But the number of repeated numbers can also be aimed at knowing the number of numbers that have had more than one occurrence in the %code% case.

Below I leave a sample script only to demonstrate how I could respond to variations in interpretation according to the specified goal, using some lists to facilitate manipulation and a single %code% .

%pre%     
______ azszpr271818 ___

You are always comparing two equal numbers so the counter ultimately has the same length value. If you do not want to change the structure of your code, I suggest making these changes:

%pre%     
______ azszpr271812 ___

You're making two errors in your code:

The first is that the loop is checking its own number, ie when i and j are the same it compares the same number. The second error is break, finding a repeated number should not break the loop.

%pre%     
______ azszpr271824 ___

If you want to use you can also extract the two information:

Find the items and do not bring the replicates:

%pre%

Group the items and count the number of replicates:

%pre%

See working at dotnetfiddle.net

    
______ azszpr271835 ___

Actually you are comparing the number of the repeat loop and not the array int [] array1 , you have to store the value of the array in a variable, and within the if

  •     
    ___

    3

    I've created a pre-defined array with some numbers, and I want to know how many repeated numbers it has in the array. but it is only returning the size of the array and not qtd of repeated numbers. and also wanted to remove the repeated numbers.

    int[] array1 = { 2, 5, 8, 10, 2, 23, 2, 4, 5, 8, 8 };
    int contador=0;
    
    for(int i = 0; i < array1.Length; i++)
    {
        for(int j = 0; j < array1.Length; j++)
        {
            if (array1[j] == array1[i])
            {
                contador++;
                break;
    
            }
        }
    }
    
    Console.WriteLine("Total de numeros repetidos -> " + contador);
    
        
    asked by anonymous 25.01.2018 / 13:59

    6 answers

    4

    Assign your array inside a HashSet, as it does not accept repeated values, and then you compare the sizes of both and know how many values were repeated:

    int[] array1 = { 2, 5, 8, 10, 2, 23, 2, 4, 5, 8, 8 };
    var meuHashSet = new HashSet<int>(array1);
    var valoresRepetidos = array1.Length - meuHashSet.Count;
    
        
    25.01.2018 / 14:10
    1

    As commented,

      

    Manieiro :   Set "how many numbers repeated", this can be interpreted as   various forms.

    The proposed answer presents a solution that depends exactly on which question is being asked.

      

    Gabriel Coletta

    int[] array1 = { 2, 5, 8, 10, 2, 23, 2, 4, 5, 8, 8 };
    var meuHashSet = new HashSet<int>(array1);
    var valoresRepetidos = array1.Length - meuHashSet.Count;
    

    The result is 5 , a result of the original size difference of Array subtracted from its Distinct , which can also be written as Linq

    int[] array1 = { 2, 5, 8, 10, 2, 23, 2, 4, 5, 8, 8 };
    var quantidadeRepetidos = array1.Length - array1.Distinct().Count();
    

    However, we come to a subjective point, 5 actually represents the number of repetitions of the values after their first occurrences, in other words, 5 is equal amount of duplicates or recurrences.

    But when we look at the original set [2, 5, 8, 10, 2, 23, 2, 4, 5, 8, 8] , we can see that it has 8 elements of repeated values (which have more than one occurrence). What can be seen more easily by rearranging and removing unique occurrences [2,2,2,5,5,8,8,8] . And this difference greatly impacts the purpose of the query and what you intend to do with that result.

    But the number of repeated numbers can also be aimed at knowing the number of numbers that have had more than one occurrence in the [2,5,8] case.

    Below I leave a sample script only to demonstrate how I could respond to variations in interpretation according to the specified goal, using some lists to facilitate manipulation and a single for() .

    int[] arrayOrignal = { 2, 5, 8, 10, 2, 23, 2, 4, 5, 8, 8 };
    
    var listUnicos = new List<int>();
    var listRepetem = new List<int>();
    var listDuplicatas = new List<int>();
    
    int tamanho = arrayOrignal.Length;
    
    for (int i = 0; i < tamanho; i++)
    {
        var elemento = arrayOrignal[i];
    
        if (arrayOrignal.Where(x => x.Equals(elemento)).Count().Equals(1))
            listUnicos.Add(elemento);
        else if (!listRepetem.Contains(elemento))
            listRepetem.Add(elemento);
        else
            listDuplicatas.Add(elemento);
    }
    
    //[2, 5, 8, 10, 23, 4] -> 6 valores distintos
    int[] arrayDistinto = arrayOrignal.Distinct().ToArray();
    
    //[10, 23, 4] -> 3 valoes únicos
    int[] arrayUnicos = listUnicos.ToArray();
    
    //[2, 5, 8] ->  3 valores que repetem 
    int[] arrayRepetem = listRepetem.ToArray();
    
    //[2, 2, 5, 8, 8] -> 5 valores recorrentes
    int[] arrayDuplicatas = listDuplicatas.ToArray();
    
    //[2, 5, 8, 2, 2, 5, 8, 8] -> 8 valores repetidos
    int[] arrayRepetidos = arrayOrignal.Where(x => listDuplicatas.Contains(x)).ToArray();
    
        
    25.01.2018 / 17:56
    0

    You are always comparing two equal numbers so the counter ultimately has the same length value. If you do not want to change the structure of your code, I suggest making these changes:

        int[] array1 = { 2, 5, 8, 10, 2, 23, 2, 4, 5, 8, 8 };
        int contador=0;
    
        for(int i = 0; i < array1.Length; i++)
        {
            for(int j = i + 1; j < array1.Length; j++)
            {
                if (j != i && array1[j] == array1[i])
                {
                    contador++;
                    break;
                }
            }
        }
    
        Console.WriteLine("Total de numeros repetidos -> " + contador); //R:5 = 2, 2, 5, 8, 8
    
        
    25.01.2018 / 14:27
    0

    You're making two errors in your code:

    The first is that the loop is checking its own number, ie when i and j are the same it compares the same number. The second error is break, finding a repeated number should not break the loop.

    int[] array1 = { 2, 5, 8, 10, 2, 23, 2, 4, 5, 8, 8 };
    int contador=0;
    
    for(int i = 0; i < array1.Length; i++)
    {
        for(int j = i; j < array1.Length; j++)
        {
            if (i != j && array1[j] == array1[i])
                contador++;
        }
    }
    
    Console.WriteLine("Total de numeros repetidos -> " + contador);
    
        
    25.01.2018 / 14:11
    0

    If you want to use you can also extract the two information:

    Find the items and do not bring the replicates:

    int[] array1 = { 2, 5, 8, 10, 2, 23, 2, 4, 5, 8, 8 };
    
    int[] arraySemRepeticao = array1.Distinct().ToArray();
    

    Group the items and count the number of replicates:

    int[] array1 = { 2, 5, 8, 10, 2, 23, 2, 4, 5, 8, 8 };
    
    var arrayAgrupadosRepeticaoCount = array1
                 .GroupBy(x => x)
                 .Select(a => new
                   {
                      Item = a.Key,
                      Quant = a.Count()
                   })
                 .ToArray();
    

    See working at dotnetfiddle.net

        
    25.01.2018 / 14:55
    -1

    Actually you are comparing the number of the repeat loop and not the array int [] array1 , you have to store the value of the array in a variable, and within the if

  •     
    25.01.2018 / 15:47