Skip to main content
added 1156 characters in body
Source Link
webcpu
  • 3.2k
  • 13
  • 18

Solution #1:

The key idea is to transform a list of the base n digits to an integer.

The advantage of the solution is fast.

In:

enter image description here

Solution #2

The key idea is to transform a list of number/symbol to a string.

In:

  ListUnion[xss_] := Module[{base, ZERO, yss},
  rotateLeftList[xs_] := 
   NestList[RotateLeft, xs, Length[xs]] // Map[ToString];
  rotateList[xs_] := {xs, Reverse[xs]} // Map[rotateLeftList] // 
    Catenate;
  xss // Map[First@*Sort@*rotateList] // Union // ToExpression
  ]

RandomSeed[3];
(*original list *)
xss = {{1, 1, 2, 1, 1, 2}, {1, 2, 1, 1, 2, 1}, {1, 
   2, 2, 1, 2, 2}, {1, 2, 2, 2, 1, 3}, {1, 2, 3, 1, 2, 3}, {1, 3, 1, 
   2, 2, 2}, {1, 3, 2, 1, 3, 2}, {2, 2, 1, 2, 2, 1}, {2, 2, 1, 3, 1, 
   2}, {2, 2, 2, 1, 3, 1}, {2, 3, 1, 2, 3, 1}}
ListUnion[xss]

(* number list *)
xss = RandomInteger[{-1, 0}, {10, 6}]
ListUnion[xss]

(* symbol list *)
xss = RandomChoice[{a, b}, {10, 6}]
ListUnion[xss]

Out: enter image description here

In:

enter image description here

Solution #1:

The key idea is to transform a list of the base n digits to an integer.

The advantage of the solution is fast.

In:

enter image description here

Solution #2

The key idea is to transform a list of number/symbol to a string.

In:

  ListUnion[xss_] := Module[{base, ZERO, yss},
  rotateLeftList[xs_] := 
   NestList[RotateLeft, xs, Length[xs]] // Map[ToString];
  rotateList[xs_] := {xs, Reverse[xs]} // Map[rotateLeftList] // 
    Catenate;
  xss // Map[First@*Sort@*rotateList] // Union // ToExpression
  ]

RandomSeed[3];
(*original list *)
xss = {{1, 1, 2, 1, 1, 2}, {1, 2, 1, 1, 2, 1}, {1, 
   2, 2, 1, 2, 2}, {1, 2, 2, 2, 1, 3}, {1, 2, 3, 1, 2, 3}, {1, 3, 1, 
   2, 2, 2}, {1, 3, 2, 1, 3, 2}, {2, 2, 1, 2, 2, 1}, {2, 2, 1, 3, 1, 
   2}, {2, 2, 2, 1, 3, 1}, {2, 3, 1, 2, 3, 1}}
ListUnion[xss]

(* number list *)
xss = RandomInteger[{-1, 0}, {10, 6}]
ListUnion[xss]

(* symbol list *)
xss = RandomChoice[{a, b}, {10, 6}]
ListUnion[xss]

Out: enter image description here

added 369 characters in body
Source Link
webcpu
  • 3.2k
  • 13
  • 18

In:

ListUnion[xss_] := Module[{base, ZERO, yss},
  rotateLeftList[xs_] := 
   NestList[RotateLeft, xs, Length[xs]] // 
    Map[FromDigits[#, base] &];
  rotateList[xs_] := {xs, Reverse[xs]} // Map[rotateLeftList] // 
    Catenate;
  
  base = If[Max[xss] < 10, 11, Max[xss] + 2];
  ZERO = (base - 1);
  yss = xss /. 0 -> ZERO // Map[First@*Sort@*rotateList] // Union // 
    IntegerDigits[#, base] & ;
  yss /. ZERO -> 0]

RandomSeed[3];
(*original list *)
xss = {{1, 1, 2, 1, 1, 2}, {1, 2, 1, 1, 2, 1}, {1, 
   2, 2, 1, 2, 2}, {1,
     2, 2, 2, 1, 3}, {1, 2, 3, 1, 2, 3}, {1, 3, 1, 
   2, 2, 2}, {1, 3, 2,
     1, 3, 2}, {2, 2, 1, 2, 2, 1}, {2, 2, 1, 3, 1, 
   2}, {2, 2, 2, 1, 3,
     1}, {2, 3, 1, 2, 3, 1}};
(*xss=RandomInteger[{1,99},{5000,10}];*)ListUnion[xss]

base = If[Max[xss] < 10, 10,(*0 Max[xss]leading +list 1];
*)
rotateLeftList[xs_]xss := 
 NestList[RotateLeft, xsRandomInteger[{0, Length[xs]] // Map[FromDigits[#1}, base] &]
rotateList[xs_] := {xs10, Reverse[xs]6} // Map[rotateLeftList] // Catenate]
xss // Map[First@*Sort@*rotateList] // Union // ListUnion[xss]
 IntegerDigits[#, base] &

Out:

{{1,(*Multiple 1,digits 2,number 1,list 1,*)
xss 2},= RandomInteger[{1, 2, 2, 1, 210, 211}, {1, 2, 2, 2, 110, 36}, {1, 2, 3,]
   1, 2, 3}}ListUnion[xss]

Out:

enter image description here

In:

xss = {{1, 1, 2, 1, 1, 2}, {1, 2, 1, 1, 2, 1}, {1, 2, 2, 1, 2, 2}, {1,
     2, 2, 2, 1, 3}, {1, 2, 3, 1, 2, 3}, {1, 3, 1, 2, 2, 2}, {1, 3, 2,
     1, 3, 2}, {2, 2, 1, 2, 2, 1}, {2, 2, 1, 3, 1, 2}, {2, 2, 2, 1, 3,
     1}, {2, 3, 1, 2, 3, 1}};
(*xss=RandomInteger[{1,99},{5000,10}];*)

base = If[Max[xss] < 10, 10, Max[xss] + 1];

rotateLeftList[xs_] := 
 NestList[RotateLeft, xs, Length[xs]] // Map[FromDigits[#, base] &]
rotateList[xs_] := {xs, Reverse[xs]} // Map[rotateLeftList] // Catenate
xss // Map[First@*Sort@*rotateList] // Union // 
 IntegerDigits[#, base] &

Out:

{{1, 1, 2, 1, 1, 2}, {1, 2, 2, 1, 2, 2}, {1, 2, 2, 2, 1, 3}, {1, 2, 3,
   1, 2, 3}}

In:

ListUnion[xss_] := Module[{base, ZERO, yss},
  rotateLeftList[xs_] := 
   NestList[RotateLeft, xs, Length[xs]] // 
    Map[FromDigits[#, base] &];
  rotateList[xs_] := {xs, Reverse[xs]} // Map[rotateLeftList] // 
    Catenate;
  
  base = If[Max[xss] < 10, 11, Max[xss] + 2];
  ZERO = (base - 1);
  yss = xss /. 0 -> ZERO // Map[First@*Sort@*rotateList] // Union // 
    IntegerDigits[#, base] & ;
  yss /. ZERO -> 0]

RandomSeed[3];
(*original list *)
xss = {{1, 1, 2, 1, 1, 2}, {1, 2, 1, 1, 2, 1}, {1, 
   2, 2, 1, 2, 2}, {1, 2, 2, 2, 1, 3}, {1, 2, 3, 1, 2, 3}, {1, 3, 1, 
   2, 2, 2}, {1, 3, 2, 1, 3, 2}, {2, 2, 1, 2, 2, 1}, {2, 2, 1, 3, 1, 
   2}, {2, 2, 2, 1, 3, 1}, {2, 3, 1, 2, 3, 1}}
ListUnion[xss]

(*0 leading list *)
xss = RandomInteger[{0, 1}, {10, 6}]
ListUnion[xss]

(*Multiple digits number list *)
xss = RandomInteger[{10, 11}, {10, 6}]
ListUnion[xss]

Out:

enter image description here

added 136 characters in body
Source Link
webcpu
  • 3.2k
  • 13
  • 18

In:

xss = {{1, 1, 2, 1, 1, 2}, {1, 2, 1, 1, 2, 1}, {1, 2, 2, 1, 2, 2}, {1,
     2, 2, 2, 1, 3}, {1, 2, 3, 1, 2, 3}, {1, 3, 1, 2, 2, 2}, {1, 3, 2,
     1, 3, 2}, {2, 2, 1, 2, 2, 1}, {2, 2, 1, 3, 1, 2}, {2, 2, 2, 1, 3,
     1}, {2, 3, 1, 2, 3, 1}};
(*xss=RandomInteger[{1,99},{5000,10}];*)

base = If[Max[xss] < 10, 10, Max[xss] + 1];

rotateLeftList[xs_] := 
 NestList[RotateLeft, xs, Length[xs]] // Map[FromDigits]Map[FromDigits[#, base] &]
rotateList[xs_] := {xs, Reverse[xs]} // Map[rotateLeftList] // Catenate
xss // Map[First@*Sort@*rotateList] // Union // IntegerDigits
 IntegerDigits[#, base] &

Out:

{{1, 1, 2, 1, 1, 2}, {1, 2, 2, 1, 2, 2}, {1, 2, 2, 2, 1, 3}, {1, 2, 3,
   1, 2, 3}}

In:

xss = {{1, 1, 2, 1, 1, 2}, {1, 2, 1, 1, 2, 1}, {1, 2, 2, 1, 2, 2}, {1,
     2, 2, 2, 1, 3}, {1, 2, 3, 1, 2, 3}, {1, 3, 1, 2, 2, 2}, {1, 3, 2,
     1, 3, 2}, {2, 2, 1, 2, 2, 1}, {2, 2, 1, 3, 1, 2}, {2, 2, 2, 1, 3,
     1}, {2, 3, 1, 2, 3, 1}};
rotateLeftList[xs_] := 
 NestList[RotateLeft, xs, Length[xs]] // Map[FromDigits]
rotateList[xs_] := {xs, Reverse[xs]} // Map[rotateLeftList] // Catenate
xss // Map[First@*Sort@*rotateList] // Union // IntegerDigits

Out:

{{1, 1, 2, 1, 1, 2}, {1, 2, 2, 1, 2, 2}, {1, 2, 2, 2, 1, 3}, {1, 2, 3,
   1, 2, 3}}

In:

xss = {{1, 1, 2, 1, 1, 2}, {1, 2, 1, 1, 2, 1}, {1, 2, 2, 1, 2, 2}, {1,
     2, 2, 2, 1, 3}, {1, 2, 3, 1, 2, 3}, {1, 3, 1, 2, 2, 2}, {1, 3, 2,
     1, 3, 2}, {2, 2, 1, 2, 2, 1}, {2, 2, 1, 3, 1, 2}, {2, 2, 2, 1, 3,
     1}, {2, 3, 1, 2, 3, 1}};
(*xss=RandomInteger[{1,99},{5000,10}];*)

base = If[Max[xss] < 10, 10, Max[xss] + 1];

rotateLeftList[xs_] := 
 NestList[RotateLeft, xs, Length[xs]] // Map[FromDigits[#, base] &]
rotateList[xs_] := {xs, Reverse[xs]} // Map[rotateLeftList] // Catenate
xss // Map[First@*Sort@*rotateList] // Union // 
 IntegerDigits[#, base] &

Out:

{{1, 1, 2, 1, 1, 2}, {1, 2, 2, 1, 2, 2}, {1, 2, 2, 2, 1, 3}, {1, 2, 3,
   1, 2, 3}}
Source Link
webcpu
  • 3.2k
  • 13
  • 18
Loading