Here are two variants on the same idea of using Nearest. The first is a little faster on small lists and on large lists. They're about the same on @Martin Ender's example in his answer.
Flatten[Nearest[Join[#1, #2] -> Join[#2, #1] & @@ Transpose@list,
order, {1, 0}], 1]
Flatten[Nearest[Flatten[list, 1] -> Flatten[Reverse /@ list, 1],
order, {1, 0}], 1]
(*
{{239, 90}, {165, 25}, {357, 120}, {337, 30}}
{{239, 90}, {165, 25}, {357, 120}, {337, 30}}
*)
Timings on @Martin Ender's example. Note that the uniqueness of the result depends on the uniqueness of the sorting keys in order. For RandomInteger[400, {2000, 2, 2}] with SeedRandom[0], the keys were not unique, so I increased the size of the integers to make them so.
SeedRandom[0];
list = RandomInteger[4000, {2000, 2, 2}];
order = RandomSample[RandomChoice /@ list];
foo1 = Flatten[
Nearest[Join[#1, #2] -> Join[#2, #1] & @@ Transpose@list,
order, {1, 0}], 1]; // RepeatedTiming
foo2 = Flatten[Nearest[Flatten[list, 1] -> Flatten[Reverse /@ list, 1],
order, {1, 0}], 1]; // RepeatedTiming
foo3 = FirstCase[list, {a_, #} | {#, a_} :> a] & /@ (* Martin Ender's faster one *)
order; // RepeatedTiming
foo1 == foo2 == foo3
(*
{0.0024, Null}
{0.0024, Null}
{1.955, Null}
True
*)
Addendum.
Note that part of the slowness of FirstCase is that it unpacks list (repeatedly). A single unpacking of list does not take that much time compared to the overall time. It makes a difference whether the call to FirstCase[] is by reference to the stored array, listup below, or is directly on the output of FromPackedArray. It seems in the latter case, the array is repacked and we're back to situation above, FirstCase[list,...]. Even weirder is the case where the array is unpacked to level 1. There were many warnings from On@"Packing" of unpacking to level 1, so I thought I'd see if an unpacked list of packed arrays might be faster. Instead it was much slower. Further, it depends on how it's unpacked, whether it's unpack and repacked at level 1 or simply unpacked with {##} & @@ list. The unpacking and repacking takes very little time. I'm not sure why the speed is so slow, but I thought it was worth pointing out. There is a similar difference whether FirstCase[] is called on an array stored in a symbol or the direct output of a function.
(* Completely unpacked *)
(listup = Developer`FromPackedArray@list;
FirstCase[listup, {a_, #} | {#, a_} :> a] & /@ order); // RepeatedTiming
FirstCase[Developer`FromPackedArray[list], {a_, #} | {#, a_} :> a] & /@
order; // RepeatedTiming
(*
{0.604, Null}
{1.9, Null}
*)
(* Unpacked to level 1 *)
(murf = {##} & @@ list; (* store in murf *)
FirstCase[murf, {a_, #} | {#, a_} :> a] & /@ order); // RepeatedTiming
FirstCase[ (* not stored in variable *)
{##} & @@ list,
{a_, #} | {#, a_} :> a] & /@ order; // RepeatedTiming
FirstCase[ (* unpacked & repacked below level 1 *)
With[{opts = SystemOptions["CompileOptions"]},
Internal`WithLocalSettings[
SetSystemOptions["CompileOptions" -> "MapCompileLength" -> Infinity],
Developer`ToPackedArray /@ Developer`FromPackedArray[list],
SetSystemOptions[opts]
]],
{a_, #} | {#, a_} :> a] & /@ order; // RepeatedTiming
(*
{1.37, Null}
{2.0, Null}
{5.7, Null}
*)
listis a list of pairs, and theordercontains one element from each pair in some order, and you want the corresponding element from all of those pairs inlist, is that correct? – Martin Ender Mar 18 '16 at 09:48list's every element (list of pairs) contain a element oforder. – yode Mar 18 '16 at 09:52