Problem
Check collection contains the same number multiple times.
If it does contain the same numbers more than once, then I want to keep the first number and give a new value to the rest of the numbers, which are the same as the first one.
list newFitnessList
does contain These numbers:
0. 4054.230995 --> after code= 4054.230995
1. 4041.416004 --> after code= 4041.416004
2. 3926.227397 --> after code= 3926.227397
3. 4722.250903 --> after code= 4722.250903
4. 4722.250903 --> after code= 0
5. 4226.636776 --> after code= 4226.636776
6. 4061.499026 --> after code= 4061.499026
7. 3876.278254 --> after code= 3876.278254
8. 4041.416004 --> after code= 0
9. 4779.468077 --> after code= 4779.468077
10. 4226.636776 --> after code= 0
11. 3876.278254 --> after code= 0
12. 4779.468077 --> after code= 0
13. 3926.227397 --> after code= 0
To achieve the solution explained above I tried the following code, but nothing is happening. The Output of the list is the same as before:
public List<double> sortDoppelganger(List<double> inputFitnessList)
{
List<double> newFitnessList = inputFitnessList.ToList();
for(int i = 0; i < newFitnessList.Count; i++)
{
double Nothing=0;
double actual = newFitnessList[i];
for(int j = newFitnessList.Count-1; j >= 0; j--)
{
double next = newFitnessList[j];
if(actual == next)
{
next = Nothing;
}
}
}
return newFitnessList;
}
I would really appreciate it if someone has any idea what's wrong with my code. And maybe it would be better not to hide, that I'm a newbie with programming.
After reading the explanations: I tried out two idea's explained. First one was the idea of @Tim Schmelter and the second idea was from @user3185569.
And here you can take a look at what i was tyring to achieve:
Here the suggestion of Tim:
Here the Suggestion of user3185569:
count() method returns an integer greater than 1 , then the value exists multiple times in the list. If you need to check if the value exists multiple times in the list, ignoring case, convert both the list items and the value to lowercase.
Using Count() The python list method count() returns count of how many times an element occurs in list. So if we have the same element repeated in the list then the length of the list using len() will be same as the number of times the element is present in the list using the count().
Use the all() function to check if multiple values are in a list, e.g. if all(value in my_list for value in multiple_values): . The all() function will return True if all of the specified values are in the list and False otherwise.
You could use a HashSet<double>
to find out if there's a duplicate:
public List<double> SortDoppelganger(List<double> inputFitnessList, double replacementValue = 0)
{
HashSet<double> doppelgangerFinder = new HashSet<double>();
for (int i = 0; i < inputFitnessList.Count; i++)
{
double value = inputFitnessList[i];
bool istDoppelganger = !doppelgangerFinder.Add(value);
if (istDoppelganger)
inputFitnessList[i] = replacementValue;
}
return inputFitnessList;
}
This solution modifies the original list. If that's not desired create a copy at the beginning by using var newList = new List<double>(inputFitnessList)
.
For what it's worth, here is a generic extension method that works with any type:
public static List<T> ReplaceDuplicates<T>(this IEnumerable<T> sequence, T replacementValue)
{
HashSet<T> duplicateFinder = new HashSet<T>();
List<T> returnList = new List<T>();
foreach (T item in sequence)
{
bool isDuplicate = !duplicateFinder.Add(item);
returnList.Add(isDuplicate ? replacementValue : item);
}
return returnList;
}
Explanation: user3185569 is right, i have forgotten to mention what you've done wrong. The main problem is that you try to assign the replacement value to the local variable next
:
double next = newFitnessList[j];
if(actual == next)
{
next = Nothing;
}
It has nothing to do with value- or reference-types differences in this case. The only reason why this doesn't work is that you modify only the value of the variable. Not the value the variable was referencing before(newFitnessList[j]
). The variable does not even know that it was linked to the list. It just knows the value to store. If it was a reference type the problem would be the same. By replacing it with another value the list won't be modifed at all.
To cut a long story short, this would fix the main problem:
double next = newFitnessList[j];
if(actual == next)
{
newFitnessList[j] = Nothing;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With