Multiply two lists
a = {1, 2, 3};
b = {4, 5, 6};
Flatten[KroneckerProduct[a, b]]
{4, 5, 6, 8, 10, 12, 12, 15, 18}
Use DeleteDuplicates
to, well, delete duplicates.
DeleteDuplicates @ Flatten @ Outer[Times, a, b]
{4, 5, 6, 8, 10, 12, 15, 18}
Also, shorter but much slower
DeleteDuplicates[Times @@@ Tuples @ {a, b}]
{4, 5, 6, 8, 10, 12, 15, 18}
Note: If speed is a concern, then Outer
, KroneckerProduct
and TensorProduct
are much faster than Table
and Tuples
methods:
SeedRandom[1]
{a, b} = RandomInteger[100, {2, 1500}];
r1 = DeleteDuplicates @ Flatten @ Outer[Times, a, b]; // RepeatedTiming // First
0.0103
r2 = DeleteDuplicates[Times @@@ Tuples @ {a, b}]; // RepeatedTiming // First
0.92
r3 = DeleteDuplicates[Flatten[Table[i*j, {i, a}, {j, b}]]]; // RepeatedTiming // First
0.78
r4 = DeleteDuplicates @Flatten[KroneckerProduct[a, b]]; // RepeatedTiming // First
0.011
r5 = DeleteDuplicates[Flatten[TensorProduct[a, b]]]; // RepeatedTiming // First
0.011
r1 == r2 == r3 == r4 == r5
True
Another way to produce the result you seek is:
DeleteDuplicates[Flatten[Table[i*j, {i, a}, {j, b}]]]
{4, 5, 6, 8, 10, 12, 15, 18}
This will also work, and generalizes easily to more than two lists:
DeleteDuplicates[Flatten[TensorProduct[a, b]]]
TensorProduct
runs in time similar to KronekerProduct
and Outer
.