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 linq 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
___