I have two arrays:
string[] array1 = {"a","b","c","d","e"}
string[] array1 = {"x","y","a","b","a"}
I want to print the result like this:
a = 3
b = 2
c = 1
d = 1
e = 1
x = 1
y = 1
z = 1
I can run a loop inside the loop and find this out but is there a better way to achieve the same result?
I want to do this in plain C# without using LINQ.
You can use LINQ to accomplish this:
var counts = array1.Concat(array2)
.GroupBy(v => v)
.Select(g => new { Value=g.Key, Number=g.Count() });
foreach(var item in counts.OrderBy(i => i.Value))
Console.WriteLine("{0} = {1}", item.Value, item.Number);
Given that you want to avoid using LINQ and the extension methods for some reason, you could build your own dictionary:
var counts = new Dictionary<string, int>();
foreach(string item in array1)
{
if (counts.ContainsKey(item))
counts[item]++;
else
counts[item] = 1;
}
foreach(string item in array2)
{
if (counts.ContainsKey(item))
counts[item]++;
else
counts[item] = 1;
}
// Print out counts
foreach(var kvp in counts)
Console.WriteLine("{0} = {1}", kvp.Key, kvp.Value);
Note that this doesn't sort the results - if you need them sorted, you'd have to do that as well.
You can use Concat, GroupBy and OrderByDescending:
var both = array1.Concat(array2);
var groups = both.GroupBy(s => s).OrderByDescending(g => g.Count());
Console.Write(
String.Join(
Environment.NewLine,
groups.Select(g => String.Format("{0} = {1}", g.Key, g.Count()))));
This looks like a job for Linq:
var charCounts = array1.Concat(array2)
.GroupBy(c=>c)
.Select(g=>new Tuple<char, int>(g.Key, g.Count());
.OrderBy(t=>t.Item1);
foreach(var result in charCounts)
Console.WriteLine(String.Format("{0} = {1}", t.Item1, t.Item2));
Read through the two arrays and put them into one dictionary. Keys are the members in the array like "a", "b", etc. Values are integers as count. So, if a key, exists, you increment the count; otherwise put the key into dictionary with a value as 1.
Well, most naive implementation based on type of string would be something like this:
class Program
{
static void Main(string[] args)
{
string[] array1 = {"a", "b", "c", "d", "e"};
string[] array2 = {"x", "y", "a", "b", "a"};
var histogram = new Dictionary<string, int>();
Fill(histogram, array1);
Fill(histogram, array2);
foreach (var p in histogram)
{
Console.WriteLine("{0}={1}",p.Key,p.Value);
}
}
private static void Fill(Dictionary<string, int> histogram, string[] a)
{
foreach (string s in a)
{
if (histogram.ContainsKey(s))
histogram[s] += 1;
else
histogram[s] = 1;
}
}
}
which is build dynamic histogram, print.
another simple approach is like this, but it's of worse readability:
static void Main(string[] args)
{
string[] array1 = {"a", "b", "c", "d", "e"};
string[] array2 = {"x", "y", "a", "b", "a"};
string [] concat = new string[array1.Length+array2.Length];
Array.Copy(array1,concat,array1.Length);
Array.Copy(array2,0,concat,array1.Length,array2.Length);
Array.Sort(concat);
int pos = 0;
while(pos<concat.Length)
{
var cur = concat[pos];
int count = 0;
while ( (pos<concat.Length) && (concat[pos]==cur))
{
pos += 1;
count += 1;
}
Console.WriteLine("{0}={1}",cur,count);
}
}
generally - concat, sort, histogram on sorted data.
Related
So I have a list of items -> A, B, C, D.
C and D are included more than once, and A and B, more than twice. This list can go on and on, so we do not know how many times an item will be included.
I need to create a new list that will have the item in one column and the number of instances of that item in another column, but I do not know how to do this. I may need to use a tuple or a class, but I am not fully sure how to implement either...
What you actually need is to Group the items of your list and perform a group operation, which is Count in your case to calculate how many times does it exist.
This is how you may initialize your list:
List<string> myList = new List<string>() { "A", "B", "C", "D", "A", "B", "C", "D", "A", "B" };
and then you will group it using GroupBy function and apply the Count aggregate function on each group.
myList
.GroupBy(item => item)
.Select(g => new {Key = g.Key, Count = g.Count()})
.ToList();
This will result in the table you need.
You can try like this:
var myList = new List<String>() { "A","B", "C", "D","A","B", "C", "D", "A","B"};
var grp = myList.GroupBy( x => x );
foreach( var g in grp )
{
Console.WriteLine( "{0} {1}", g.Key, g.Count() );
}
DOTNET FIDDLE
char[] items = new[] { 'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B' };
Dictionary<char, int> counts = new();
foreach(char c in items)
{
if (counts.TryGetValue(c, out int n))
{
counts[c] = n + 1;
}
else
{
counts.Add(c, 1);
}
}
While not a one liner, a simple and fast option.
I may need to use a tuple or a class, but I am not fully sure how to implement either...
Since you mentioned you may want to use a class, here is an example:
public class TextCount
{
public string Text { get; set; }
public int Count { get; set; }
}
public class Program
{
public static void Main()
{
// Initialize the list of strings
List<string> data = new List<string> { "A", "B", "C", "D", "A", "B", "C", "D", "A", "B" };
// Use LINQ to group the strings by their value and count the number of occurrences of each string
List<TextCount> result = data
.GroupBy(s => s)
.Select(g => new TextCount { Text = g.Key, Count = g.Count() })
.ToList();
// Print the results
foreach (TextCount sc in result)
{
Console.WriteLine("{0}: {1}", sc.Text, sc.Count);
}
}
}
Demo: https://dotnetfiddle.net/2FRBbK
My English isn't that good.
I try to make a TextLogger, for example:
if I have two different arrays:
string[] array1 = {"a", "b", "c", "d"}
string[] array2 = {"y", "c", "h", "f"}
and I have the char "c" in both of the arrays, then both of the char "c" should be removed.
Output:
a, b, d, h, y, f
this is what I managed to do so far:
string[] array1 = {"a", "b", "c", "d"}
string[] array2 = {"y", "c", "h", "f"}
for(int i = 0; i < array1.Length; i++)
{
if(array1[i] == array2[i])
{
}
}
edit(sorry for keep changing my question):
and how I can do it with this:
ArrayList array1 = new ArrayList();
array1.Add("a");
array1.Add("b");
array1.Add("c");
array1.Add("d");
ArrayList array2 = new ArrayList();
array1.Add("y");
array1.Add("c");
array1.Add("h");
array1.Add("f");
1- Get common items using Enumerable.Intersect
2- replace each array by the same array except common items using Enumerable.Except
string[] array1 = { "a", "b", "c", "d" };
string[] array2 = { "y", "c", "h", "f" };
var intersect = array1.Intersect(array2); // 1
array1 = array1.Except(intersect).ToArray(); //2
array2 = array2.Except(intersect).ToArray(); //2
Edit: to take into account double values as mentioned in the comment:
string[] array1 = { "a", "b", "b", "b", "c", "d" };
string[] array2 = { "y", "b", "c", "h", "f" };
var grpArray1 = array1.GroupBy(a => a)
.Select(grp => new { item = grp.Key, count = grp.Count() });
var grpArray2 = array2.GroupBy(a => a)
.Select(grp => new { item = grp.Key, count = grp.Count() });
array1 = grpArray1.Select(a =>
{
var bCount = array2.Count(x => x.Equals(a.item));
return new { item = a.item, finalCount = a.count - bCount };
})
.Where(a => a.finalCount > 0)
.SelectMany(a => Enumerable.Repeat(a.item, a.finalCount))
.ToArray();
array2 = grpArray2.Select(a =>
{
var bCount = array1.Count(x => x.Equals(a.item));
return new { item = a.item, finalCount = a.count - bCount };
})
.Where(a => a.finalCount > 0)
.SelectMany(a => Enumerable.Repeat(a.item, a.finalCount))
.ToArray();
Console.WriteLine("-->array1:");
foreach (var item in array1)
Console.WriteLine(item);
Console.WriteLine("-->array2:");
foreach (var item in array2)
Console.WriteLine(item);
The results:
-->array1:
a
b
b
d
-->array2:
y
h
f
If if understand your problem correctly, you don't need to change that arrays (array1 and array2) but get a result from both of them.
so, you can solve your problem using the GroupBy method
string[] array1 = { "a", "b", "c", "d" };
string[] array2 = { "y", "c", "h", "f" };
var filteredArray = array1.Concat(array2).GroupBy(x => x).Where(x => x.Count() == 1).Select(x=>x.Key);
Console.WriteLine(string.Join(" ", filteredArray));
Console.ReadLine();
what we can see here, is concat the arrays into 1 list, then group by the chars into groups, and then filter the groups that contain more than 1 element, and in the end revert it into list of chars (instead of list of groups)
Edit:
out of the comment about the duplicated "b" inside each of the arrays, i created a new (with little bit more complexity) that works for your case:
string[] array1 = { "a", "b", "b", "c", "d" };
string[] array2 = { "y", "c", "h", "f" };
var filteredArray = array1.GroupBy(x => x)
.Concat(array2.GroupBy(x => x))
.GroupBy(x => x.Key)
.Where(x => x.Count() == 1).SelectMany(x => x.Key);
Console.WriteLine(string.Join(" ", filteredArray));
Console.ReadLine();
whats happen there? we group the arrays each for himself, then concat the groups together, and then we group the groups by their keys, and the filter where each group contain more then 1 inner group, and in the end we select the groups keys (in addition it's promise us there is only 1 instance of each char)
Hope that helps!
The following code snippet should provide you a clear insight about how to perform your task:
String[] array1 = new String[] {"a", "b", "c", "d"};
String[] array2 = new String[] {"y", "c", "h", "f"};
String[] n1 = array1.Where(x => !array2.Contains(x)).ToArray();
String[] n2 = array2.Where(x => !array1.Contains(x)).ToArray();
Console.WriteLine("Array 1");
foreach (String s in n1)
Console.WriteLine(s);
Console.WriteLine("\nArray 2");
foreach (String s in n2)
Console.WriteLine(s);
Console.ReadLine();
The output is:
Array 1
a
b
d
Array 2
y
h
f
and can see a working demo by visiting this link. For more information concerning the methods I used in order to accomplish, visit the following links:
Enumerable.Contains
Enumerable.ToArray
Enumerable.Where
I want to loop through the array which is the result of the array to the Cartesian power n. https://en.wikipedia.org/wiki/Cartesian_product#n-ary_product
This is what I want to achieve, just with n depth:
int[] array = new int[] { 5, -4, ... }
foreach(int a in array) {
foreach(int b in array) {
foreach(int c in array) {
...
int[] NewArray = new int[] { a, b, c, ... }
In Python this is equivalent to:
from itertools import product
for (NewArray in product(array, repeat=n)):
print(NewArray)
I do not know how I can implement this in C#.
Any help would be highly appreciated. Thanks.
You can implement a cartesian product with a little math and yield return:
static public IEnumerable<T[]> Product<T>(IList<T> items, int repeat) {
var total = (int)Math.Pow(items.Count, repeat);
var res = new T[repeat];
for (var i = 0 ; i != total ; i++) {
var n = i;
for (var j = repeat-1 ; j >= 0 ; j--) {
res[j] = items[n % items.Count];
n /= items.Count;
}
yield return res;
}
}
Calling it like this
foreach (var arr in Product(new[] {"quick", "brown", "fox"}, 3)) {
Console.WriteLine(string.Join("-", arr));
}
produces the following output:
quick-quick-quick
quick-quick-brown
quick-quick-fox
quick-brown-quick
quick-brown-brown
quick-brown-fox
quick-fox-quick
quick-fox-brown
quick-fox-fox
brown-quick-quick
brown-quick-brown
brown-quick-fox
brown-brown-quick
brown-brown-brown
brown-brown-fox
brown-fox-quick
brown-fox-brown
brown-fox-fox
fox-quick-quick
fox-quick-brown
fox-quick-fox
fox-brown-quick
fox-brown-brown
fox-brown-fox
fox-fox-quick
fox-fox-brown
fox-fox-fox
Demo.
you can calculate cartesian product of two arrays like below
string[][] CartesianProduct(string[] arr1, string[] arr2)
{
// for each s1 in arr1, extract arr2,
// then pass s1 and s2 into a newly-made string array.
return arr1.SelectMany(s1 => arr2, (s1, s2) => new string[] { s1, s2 })
.ToArray();
}
Lets say you have two array namely
string[] set1 = new string[] { "a", "b", "c" };
string[] set2 = new string[] { "x", "y", "z" };
make a call to CartesianProduct function which would return the resulting value like below.
var cartesionProduct = CartesianProduct (set1,set2);
I have a following List, I need to iterate through the list and see if the list has identical elements in it and return only a unique list. Could anyone please let me know what's wrong with the following code and a proper way to do it?
Also, Linq way to do it, if any?
Expected Solution would be = {{"a", "b", "c"},{"e", "b", "c" }}
class Program1
{
static void Main(string[] args)
{
List<string>[] stringLists = new List<string>[3]
{
new List<string>(){ "a", "b", "c" },
new List<string>(){ "e", "b", "c" },
new List<string>(){ "a", "b", "c" }
};
List<List<string>> prt = new List<List<string>>();
/* I DONT UNDERSTAND WHY THIS IS NOT WORKING !!!!!!!!!!!!!!! */
foreach (var item in stringLists)
{
for (int i = 0; i < item.Count; i++)
{
if (item == stringLists[i] && (!prt.Contains(item)))
{
prt.Add(item);
}
}
}
}
}
You can try good old Distinct with a custom IEqualityComparer<T>:
using System.Linq;
...
public class SequenceComparer<T> : IEqualityComparer<IEnumerable<T>> {
public bool Equals(IEnumerable<T> x, IEnumerable<T> y) {
return Enumerable.SequenceEqual(x, y);
}
//TODO: Suboptimal HashCode implementation
public int GetHashCode(IEnumerable<T> obj) {
return obj == null
? 0
: obj.Count();
}
}
...
var stringLists = new List<string>() {
new List<string> {"a", "b", "c"},
new List<string> {"e", "b", "c"},
new List<string> {"a", "b", "c"}
};
// All you have to do is to put Distinct
var result = stringLists
.Distinct(new SequenceComparer<string>())
.ToList(); // If you want materialization
Test:
Console.Write(string.Join(Environment.NewLine, result
.Select(list => string.Join(", ", list))));
Outcome:
a, b, c
e, b, c
You need to do union on all the list and than do a distinct.
Something like this, just iterate through the list and union it with last result:
List<string> result = new List<string>();
foreach (var list in stringLists)
{
result = result.Union(list).ToList();
}
result = result.Distinct().ToList();
prt.Contains(item) expression compares List's with their reference not by their elements, so it's a bad choice for determining if two lists are duplicates.
Try following
var stringLists = new List<string>[3]
{
new List<string> {"a", "b", "c"},
new List<string> {"e", "b", "c"},
new List<string> {"a", "b", "c"}
};
var prt = new List<List<string>>();
foreach (var item in stringLists)
{
if(prt.All(it => it.Count != item.Count || it.Except(item).Any()))
prt.Add(item);
}
Dmitry Bychenko's answer is the way to go.
For your special case and with data that does not contain , you could get away with:
using System;
using System.Linq;
using System.Collections.Generic;
public class Program1
{
public static void Main()
{
var stringLists = new List<List<string>>{
new List<string> {"a", "b", "c"},
new List<string> {"e", "b", "c"},
new List<string> {"a", "b", "c"}
};
var prt = stringLists
.Select(l => string.Join(",", l)) // make it a string separated by ,
.Distinct() // distinct it using string.Distinct()
.Select(l => l.Split(',').ToList()); // split it again at , and make it List
foreach (var p in prt)
{
foreach (var c in p)
Console.WriteLine(c);
Console.WriteLine();
}
}
}
There is lots of un-needed object creation in this approach - but i would work (until your data contains a , - then is messes your lists up).
Output:
a
b
c
e
b
c
Your misunderstanding is that you expect the statement
prt.Contains(item)
to return true when the sequence of strings in item already exists in prt. However the test internal to Contains used to determine this is a reference equality, not a item by item equality. Here is a minimal example to illustrate this:
void Main()
{
Console.Write( (new []{new []{ "a", "b", "c" }}).Contains(new[] { "a", "b", "c" }));
// Prints false
}
You either need to use a deep equals comparer like #Dmitry's answer which creates a digest (hash) of each list and compares the digests, or to do it explicitly, like this code:
class Program1
{
static void Main(string[] args)
{
List<string>[] stringLists = new List<string>[3]
{
new List<string>(){ "a", "b", "c" },
new List<string>(){ "e", "b", "c" },
new List<string>(){ "a", "b", "c" }
};
List<List<string>> prt = new List<List<string>>();
for(int i = 0; i < 3; i++)
{
bool isDifferentFromAllOthers = true;
for(int j = 0; j < i; j++)
{
bool isSameAsThisItem = true;
for(int item = 0; item < 3; item++)
{
// !!! Here is the explicit item by item string comparison
if (stringLists[i][item] != stringLists[j][item])
{
isSameAsThisItem = false;
break;
}
}
if (isSameAsThisItem)
{
isDifferentFromAllOthers = false;
break;
}
}
if (isDifferentFromAllOthers)
{
prt.Add(stringLists[i]);
}
}
// /* I DONT UNDERSTAND WHY THIS IS NOT WORKING !!!!!!!!!!!!!!! */
// foreach (var item in stringLists)
// {
// for (int i = 0; i < item.Count; i++)
// {
// if (item == stringLists[i] && (!prt.Contains(item)))
// {
// prt.Add(item);
// }
// }
// }
}
}
I'd like know if at least one element of listRef is present more than once in listA ? The other values can be present more than once.
List<string> listA = new List<string> { "A", "A", "B", "C", "D", "E" };
List<string> listRef = new List<string> { "B", "D" };
Thanks,
Try this:
bool hasRef = listref.Any(r => listA.Count(a => a == r) > 1);
I would use ToLookup method to generate Lookup<string, string> first, and then use it to check your condition:
var lookup = listA.ToLookup(x => x);
return listRef.Any(x => lookup.Contains(x) && lookup[x].Count() > 1);
You could 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);
something like this
var query = listRef.Where(x=>
listA.Where(a => a == x)
.Skip(1)
.Any());
listRef.ForEach(refEl => {
var count = listA.Count(aEl => aEl == refEl);
if(count > 1) {
//Do something
}
});
Finding the best performing option in this case is not simple because that depends on the number of items in the lists and the expected result.
Here's a way to do it that is performant in the face of big 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));
this 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
}
}
this can be another way to do
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);
var result = listA.GroupBy(x=>x)
.Where(g=>g.Count()>1&&listRef.Contains(g.Key))
.Select(x=>x.First());
bool a = result.Any();
If the second list is large and can contain duplicates i would use a HashSet<string> and IntersectWith to remove possible duplicates and strings which 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 the extension which 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 isn't large or doesn't contain duplicates you can just use:
bool anyMoreThanOne = listRef
.Any(rs => listA.ContainsMoreThanOnce(rs, StringComparison.OrdinalIgnoreCase));