Find duplicate in the list from the list of links

I would like to know if at least one listRef element is present in list A once? Other values ​​may be present multiple times.

List<string> listA = new List<string> { "A", "A", "B", "C", "D", "E" }; List<string> listRef = new List<string> { "B", "D" }; 

Thanks,

+4
source share
9 answers

Try the following:

 bool hasRef = listref.Any(r => listA.Count(a => a == r) > 1); 
+5
source

I would like to use the ToLookup method to generate Lookup<string, string> , and then use it to check your status:

 var lookup = listA.ToLookup(x => x); return listRef.Any(x => lookup.Contains(x) && lookup[x].Count() > 1); 

You can use GroupBy and ToDictionary to achieve the same:

 var groups = listA.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count()); return listRef.Any(x => groups.ContainsKey(x) && groups[x] > 1); 
+5
source

something like that

  var query = listRef.Where(x=> listA.Where(a => a == x) .Skip(1) .Any()); 
+2
source
 listRef.ForEach(refEl => { var count = listA.Count(aEl => aEl == refEl); if(count > 1) { //Do something } }); 
+1
source

Finding the best option in this case is not easy, because it depends on the number of items in the lists and the expected result.

Here is a way to do this, which is a performance in front of large lists:

 var appearances = listA.GroupBy(s => s) .Where(g => g.Count() > 1) .ToDictionary(g => g.Key, g => g.Count()); var hasItemAppearingMoreThanOnce = listRef.Any(r => appearances.ContainsKey(r)); 
+1
source

it works

  List<string> listA = new List<string> { "A", "A", "B", "C", "D", "E" }; List<string> listRef = new List<string> { "A", "D" }; foreach (var item in listRef) { if (listA.Where(x => x.Equals(item)).Count() > 1) { //item is present more than once } } 
0
source
 var result = listA.GroupBy(x=>x) .Where(g=>g.Count()>1&&listRef.Contains(g.Key)) .Select(x=>x.First()); bool a = result.Any(); 
0
source

it might be a different way

  List<string> listA = new List<string> { "A", "A", "B", "C", "D", "E" , "D" }; List<string> listRef = new List<string> { "B", "D" }; var duplicates = listA.GroupBy(s => s).SelectMany(grp => grp.Skip(1)); var newData = duplicates.Select(i => i.ToString()).Intersect(listRef); 
0
source

If the second list is large and may contain duplicates, I would use HashSet<string> and IntersectWith to remove possible duplicates and strings that are not in the first list from the second:

 var refSet = new HashSet<string>(listRef); refSet.IntersectWith(listA); bool anyMoreThanOne = refSet.Any(rs => listA.ContainsMoreThanOnce(rs, StringComparison.OrdinalIgnoreCase)); 

Here's an extension that is not very elegant but works:

 public static bool ContainsMoreThanOnce(this IEnumerable<string> coll, String value, StringComparison comparer) { if (coll == null) throw new ArgumentNullException("col"); bool contains = false; foreach (string str in coll) { if (String.Compare(value, str, comparer) == 0) { if (contains) return true; else contains = true; } } return false; } 

Demo

However, if the second listRef is small or does not contain duplicates, you can simply use:

 bool anyMoreThanOne = listRef .Any(rs => listA.ContainsMoreThanOnce(rs, StringComparison.OrdinalIgnoreCase)); 
0
source

Source: https://habr.com/ru/post/1500116/


All Articles