i need a way to compare two array and compute the Equivalence percentage so if equivalence Percentage exceed (for example 60%) do some actions used language is C# .NET 4.0
-
3How are you defining "equivalence percentage"? Is there a formula/algorithm you need to apply? – D Stanley Jun 17 '14 at 20:03
-
there is no algorithm – user3749485 Jun 17 '14 at 20:12
-
1[Please don't answer bad questions.](http://meta.stackoverflow.com/a/252531/436282) – Andrew Jun 17 '14 at 20:19
-
1How do you define equivalence? Is it number of elements that match? Does order matter? Do you want to include relative comparisons of items (that is, if you have lists of words, do you want to compare similarities between words)? Do the lengths of the lists count? Without some idea of what you mean by "equivalence," there's no way to answer your question. – Jim Mischel Jun 17 '14 at 20:37
-
it's the intersect percentage between two array – user3749485 Jun 17 '14 at 20:54
-
The answer you selected is not the intersect percentage. It's the percentage of indexes such that `a[i] == j[i]`. – Jim Mischel Jun 17 '14 at 21:19
2 Answers
The question is poorly defined, so I've taken some broad assumptions, but here's a sample implementation that measures equivalence based on element equality:
int[] a = new int[] { 1, 2, 3, 4, 5 };
int[] b = new int[] { 1, 7, 3, 4 };
int equalElements = a.Zip(b, (i, j) => i == j).Count(eq => eq);
double equivalence = (double)equalElements / Math.Max(a.Length, b.Length);
if (equivalence >= .6)
{
// 60%+ equivalent
}
Zip
: "Applies a specified function to the corresponding elements of two sequences." In this case, we're comparing each element from a
with the corresponding element from b
, and producing true
if they're equal. For example, we compare 1
with 1
, 2
with 7
, 3
with 3
, and 4
with 4
. We then count the number of equalities we encountered, storing this value into equalElements
. Finally, we divide this by the total number of elements in the larger sequence, and thus get the equivalence ratio.

- 53,759
- 13
- 140
- 188
-
-
1@user3749485 [RTFM](http://msdn.microsoft.com/en-us/library/vstudio/dd267698(v=vs.100).aspx) – tnw Jun 17 '14 at 20:19
Assuming you're comparing two int lists (or arrays, it's the same) you can compute the percentage of equivalent elements between list1
and list2
this way:
List<int> list1 = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8 };
List<int> list2 = new List<int>() { 3, 5, 8 };
var res = list1.Intersect(list2).ToList().Count();
float perc = (float)list1.Count() / res;

- 3,885
- 1
- 30
- 42
-
2@user3749485 - You haven't told anyone what you want, so how can anyone write something that "works"? – Andrew Jun 17 '14 at 20:35
-
-
-
this doesn't consider the order of the elements / duplicate elements. doesn't work for e.g. `List
list = new List – fubo Nov 05 '15 at 06:23() { true, false, false };`