Hi
I have a number of arrays of longs, from which I need to find a single
array which only contains the values which appear in all the original
arrays.
For example, I could have the three arrays:
1, 3, 2, 8, 5
3, 6, 1
6, 1, 4, 3
from which I need to generate the "common" array:
1, 3
(as these are the only values which appear in all the original arrays).
What is a good algorithm for this? The below is what I have written,
which does seem to do what I want. But more than likely there is a much
better, more efficient way to do this...
static void Main(string[] args)
{
long[] x = new long[5] { 1, 3, 2, 8, 5 };
long[] y = new long[3] { 3, 6, 1 };
long[] z = new long[4] { 6, 1, 4, 3 };
List<long[]originals = new List<long[]{ x, y, x };
long[] commonArray = Common(original s);
}
static long[] Common(List<lon g[]originals)
{
long[] temp = originals[0];
for (int i = 1; i < originals.Count ; i++)
{
temp = Common(temp, originals[i]);
}
return temp;
}
static long[] Common(long[] one, long[] two)
{
List<longcommon = new List<long>();
foreach (long val in one)
{
if (two.Contains(v al))
{
common.Add(val) ;
}
}
return common.ToArray( );
}
Thanks,
Peter
I have a number of arrays of longs, from which I need to find a single
array which only contains the values which appear in all the original
arrays.
For example, I could have the three arrays:
1, 3, 2, 8, 5
3, 6, 1
6, 1, 4, 3
from which I need to generate the "common" array:
1, 3
(as these are the only values which appear in all the original arrays).
What is a good algorithm for this? The below is what I have written,
which does seem to do what I want. But more than likely there is a much
better, more efficient way to do this...
static void Main(string[] args)
{
long[] x = new long[5] { 1, 3, 2, 8, 5 };
long[] y = new long[3] { 3, 6, 1 };
long[] z = new long[4] { 6, 1, 4, 3 };
List<long[]originals = new List<long[]{ x, y, x };
long[] commonArray = Common(original s);
}
static long[] Common(List<lon g[]originals)
{
long[] temp = originals[0];
for (int i = 1; i < originals.Count ; i++)
{
temp = Common(temp, originals[i]);
}
return temp;
}
static long[] Common(long[] one, long[] two)
{
List<longcommon = new List<long>();
foreach (long val in one)
{
if (two.Contains(v al))
{
common.Add(val) ;
}
}
return common.ToArray( );
}
Thanks,
Peter
Comment