0

I have a List contains arrays in various sizes. For example:

enter image description here

  • 0 and 7. arrrays have same data {1,2,3,4,5,6}

  • 2, 4 and 5. arrays have same data {1,2,3}

  • Attention! 3 and 6 doesn't have same data [3] = {1,2} , [6] = {1,3}

I want to get which indexes have same data and add this indexes to another List. For example

anotherList[ 0 ] = {0,7}

anotherList[ 1 ] = {2,4,5}

How can I do this?

Thanks in advance.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
1teamsah
  • 1,863
  • 3
  • 23
  • 43
  • 7
    What did you try so far? – Thomas Weller Dec 27 '13 at 14:33
  • 1
    I imagine you would loop through your list, with a nested loop through the other elements in the list, comparing each element and adding indexes to another list each time the comparison found a match. The comparison itself would be a method which accepts two arrays and determines if they're "equal" by the logic you've defined. Have you made any attempt at this? – David Dec 27 '13 at 14:35
  • 1
    http://stackoverflow.com/questions/713341/comparing-arrays-in-c-sharp – vborutenko Dec 27 '13 at 14:35
  • You want to make a new `List` that `contains` the values requested; e.g. `anotherList[0] = {0,7}` will return a new `List` that tells you which `List` `contains` both `0` and `7`??? – KSdev Dec 27 '13 at 14:35
  • To simplify the code i would suggest to have a look at the ISet Implementations like HashSet with method to compare items for example Intersect wich returns the items contained in both lists http://msdn.microsoft.com/de-de/library/vstudio/bb918911%28v=vs.90%29.aspx – Boas Enkler Dec 27 '13 at 14:41

3 Answers3

1

You can use this code (Linq i.e. SequenceEqual is very helpful here):

    private static IList<IList<int>> EqualArrays(List<int[]> list) {
      IList<IList<int>> result = new List<IList<int>>();

      HashSet<int> proceeded = new HashSet<int>();

      for (int i = 0; i < list.Count; ++i) {
        if (proceeded.Contains(i))
          continue;

        int[] item = list[i];
        List<int> equals = new List<int>() { i };

        result.Add(equals);

        for (int j = i + 1; j < list.Count; ++j)
          if (item.SequenceEqual(list[j])) {
            equals.Add(j);
            proceeded.Add(j);
          }
      }

      return result;
    }


   ...

   // Your test case:
   List<int[]> list = new List<int[]>() {
     new int[] {1, 2, 3, 4, 5, 5, 7},
     new int[] {1},
     new int[] {1, 2, 3},
     new int[] {1, 2},
     new int[] {1, 2, 3},
     new int[] {1, 2, 3},
     new int[] {1, 3},
     new int[] {1, 2, 3, 4, 5, 5, 7}
   };

   // anotherList == {{0, 7}, {1}, {2, 4, 5}, {3}, {6}}
   IList<IList<int>> anotherList = EqualArrays(list);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

Try this

List<byte[]> anotherList = new List<byte[]>();
foreach (byte[] array in list2)
    if (!list1.Any(a => a.SequenceEqual(array)))
        anotherList.Add(array);
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
0
int[][] original =
{
    new [] {1,2,3,4,5,6},
    new [] {1},
    new [] {1,2,3},
    new [] {1,2},
    new [] {1,2,3},
    new [] {1,2,3},
    new [] {1,3},
    new [] {1,2,3,4,5,6},
};

int[][] anotherList =
    original.Select((values, index) => new { values, index })
            .GroupBy(x => x.values, SequenceComparer<int>.Default)
            .Where(grouping => grouping.Count() > 1)   // optional
            .Select(grouping => grouping.Select(x => x.index).ToArray())
            .ToArray();

I've adapted the definition for SequenceComparer<T> from here (and here):

public class SequenceComparer<T> : IEqualityComparer<IEnumerable<T>>
{
    public static readonly SequenceComparer<T> Default = new SequenceComparer<T>();

    public bool Equals(IEnumerable<T> x, IEnumerable<T> y)
    {
        if (Object.ReferenceEquals(x, y))
            return true;

        return x != null && y != null && x.SequenceEqual(y);
    }

    public int GetHashCode(IEnumerable<T> seq)
    {
        if (seq == null)
            return 0;

        unchecked
        {
            const int p = 16777619;
            const int hash = (int)2166136261;

            return seq.Select(e => e.GetHashCode())
                      .Aggregate(hash, (a, b) => (a ^ b) * p));
        }
    }
}
Community
  • 1
  • 1
Douglas
  • 53,759
  • 13
  • 140
  • 188