How to remove duplicate elements from an array after combining several arrays?

Hi all,

I have several arrays in these format:

a=[
[10,2,3,2012,],
[20,3,4,2012],
[3,4,5,2012],

]

b=[
[1,2,3,2013,],
[2,3,4,2013],
[3,4,5,2013],
]

c=[
[2,3,4,2014],
[4,5,6,2014],
[5,6,7,2014],

]

d=a+b+c

By definition 1) [3,4,5,2012] and [3,4,5,2013] are duplicated,
2) [2,3,4,2013] and [2,3,4,2014] are also duplicated

and the rule is that elements will be considered duplicated if the
first two columns in each element are identical

I know I can use this line to get the unique elements if I only filter
by one column: d.uniq!{|array|array[0]}

But this time my filter conditions are based on array[0] and array[1],
how can I write a code line to achieve this purpose?

Also I want to keep the elements from 2013 only after removing
duplicates, how can I do that? e.g., [3,4,5,2012] and [3,4,5,2013] are
duplicated, keep [3,4,5,2013] after removing duplicate ;
[2,3,4,2013] and [2,3,4,2014] are duplicated, keep [2,3,4,2013] after
removing duplicate.

Thanks in advance.

You can try to

  1. iterate over all elements and create a hash with the
    duplication-criteria as the key and an array of affected elements as the
    values.
  2. iterate over each such hash-value containing more than 1 entry to
    determine which one is to go.
  3. remove the identified bad elements from the original array.

I find this in Ruby way: #sort_by or #uniq by multiple parameters:
just put all the parameters in an array as follows:

d.uniq!{|array|[array[0],array[1]]}

also if I want to keep those from 2013 after removing duplicates, just
put that array in front of the others when combining all arrays. For
this example

d=b+a+c

or d=b+c+a

everything in b will be kept after removing duplicates.

Li CN wrote in post #1158635:

and the rule is that elements will be considered duplicated if the
first two columns in each element are identical

Also I want to keep the elements from 2013 only after removing
duplicates, how can I do that? e.g., [3,4,5,2012] and [3,4,5,2013] are
duplicated, keep [3,4,5,2013] after removing duplicate ;
[2,3,4,2013] and [2,3,4,2014] are duplicated, keep [2,3,4,2013] after
removing duplicate.

p d.sort_by {|a| -a.last}.uniq {|(a,b,c)| [a,b]}

[[5, 6, 7, 2014],
[4, 5, 6, 2014],
[2, 3, 4, 2014],
[1, 2, 3, 2013],
[3, 4, 5, 2013],
[20, 3, 4, 2012],
[10, 2, 3, 2012]
]