0
$\begingroup$

Suppose, I have a list of lists where multiple lists can have same elements. In my final output, I just want one list with the same element. An example is the following:

Input: A=[[1,2,0], [0,1,2], [2,1,0], [4,5,6], [6,5,4]]

Desired Output: $[[0,1,2], [4,5,6]]$

Can someone help me with an easy function for doing so? Thank you so much.

$\endgroup$
3
  • 2
    $\begingroup$ What about [[1, 2], [2, 3]]? $\endgroup$
    – greybeard
    Commented Mar 1, 2023 at 6:22
  • 3
    $\begingroup$ Your question is not clear. Please specify exactly what you want the output to be in general. One example is not a substitute for a general problem specification. "one list with the same element" is not clear. Same as what? You mention only one element, but the example shows multiple elements. $\endgroup$
    – D.W.
    Commented Mar 1, 2023 at 18:17
  • $\begingroup$ In fact, you are seeing your lists as (multi)sets. $\endgroup$
    – user16034
    Commented Jul 29, 2023 at 11:43

2 Answers 2

1
$\begingroup$

Sort every sublist, then sort the main list with lexicographical ordering. The duplicates will appear next to each other.

$$[[1,2,0], [6,5,4], [0,1,2], [2,1,0], [4,5,6]]$$ then $$[[0,1,2], [4,5,6], [0,1,2], [0,1,2], [4,5,6]]$$ then $$[[0,1,2], [0,1,2], [0,1,2], [4,5,6], [4,5,6]]$$

$\endgroup$
0
$\begingroup$

This appears to be an instance of the set cover problem. There is no efficient algorithm, but you can read about standard algorithms.

$\endgroup$
1
  • 4
    $\begingroup$ It could be that, or it could just be removal of duplicates. $\endgroup$
    – Pseudonym
    Commented Mar 1, 2023 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.