A list check contains the same value several times.

Problem

A collection check contains the same number several times.

If it contains the same digits more than once , then I want to save the first number and give a new value to the remaining numbers that match the first.

list newFitnessList contains 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 find the solution described above, I tried the following code, but nothing happens. The list output is similar to the previous one:

 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 thought about what happened to my code. And maybe it's better not to hide the fact that I'm new to programming.

After reading the explanation: I tried to explain two ideas. First there was the idea of ​​@Tim Schmelter, and the second idea was from the user @ user3185569.

And here you can take a look at what I was typical for achieving: enter image description here

Here is Tim's suggestion: enter image description here

Here is user suggestion 3185569: enter image description here

+6
source share
5 answers

You can use the HashSet<double> to find out if there is 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 this is not desired, create a copy at the beginning using var newList = new List<double>(inputFitnessList) .

For what it's worth, here is a general 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 forgot to mention what you did wrong. The main problem is that you are trying to assign the replacement value to the next local variable:

 double next = newFitnessList[j]; if(actual == next) { next = Nothing; } 

In this case, this has nothing to do with differences in values ​​or reference types. The only reason this does not work is because you only change the value of the variable. Not the value that the variable previously indicated ( newFitnessList[j] ). The variable does not even know that it was associated with the list. He just knows the value of storage. If it were a reference type, the problem would be the same. Replacing it with a different value will not change the list at all.

To shorten the long story, this will fix the underlying problem:

 double next = newFitnessList[j]; if(actual == next) { newFitnessList[j] = Nothing; } 
+8
source

Problems with your code:

1- next is the local variable value-Type , so you are not actually changing anything inside the list.

2- You need to skip the case when i == j , or you will get all your values ​​as 0 (in itself)

 public static 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--) { if (j == i) continue; double next = newFitnessList[j]; if (actual == next) { newFitnessList[j] = Nothing; } } } return newFitnessList; } 

You may need to replace if (actual == next) with (Math.Abs(actual - next) < 0.0001) since you cannot trust double with exact comparisons.

Using:

 var newList = sortDoppelganger(list); 

What can be learned from your error:

When you write double next = newFitnessList[j]; , you are actually copying the value in this list index into a local variable named next . Since the type is double (A Value-Type), changing next does not change anything inside your list.

However, if you use a reference type (for example, the Customer class instead of double), changing the value of the next properties will change them inside the list.

+2
source

Nothing 0 , so it sets the List values ​​to 0 .

You can use this to check for duplicates:

 bool areDduplicates = lstNames.GroupBy(n => n).Any(c => c.Count() > 1); 

From here .

0
source
 private static IEnumerable<double> sortDoppelganger(List<double> doubles) { var newList = new List<double>(); foreach (var t in doubles) { newList.Add(newList.Contains(t) ? 0.0 : t); } return newList; } 
0
source
 public List<double> filterlist(List<double> lst) { List<double> lstDouble = new List<double>(); for (int i = 0; i < lst.Count; i++) { if (lstDouble.Contains(lst[i])) { lstDouble.Add(0); } else { lstDouble.Add(lst[i]); } } return lstDouble; } 
0
source

All Articles