# How to remove repeated lists and get only one of them? like: [[3,7], [7,3]]

This is output
[[3, 2, 2, 3], [5, 2, 3], [3, 4, 3], [7, 3], [5, 2, 3], [3, 5, 2], [3, 7], [3, 5, 2]]

This i want
[[3, 7], [3, 2, 5], [3, 4, 3], [3, 2, 2, 3]]

like: [[3,7], [7,3]] → sum of the element in both subsets is 10 but i want only one of them [3,7] or [7,3] in my output.*

`````` defmodule Test do
@moduledoc false

def possible_subsets(list, sum) do
list
|> subsets()
|> Enum.filter(&(Enum.sum(&1) == sum))
end

defp subsets([]), do: [[]]
defp subsets([h | t]) do
t
|> subsets()
|> Enum.flat_map(&[[h | &1], &1])
end

end
``````

One common approach for handling situations where you want to compare lists without considering order is to use `MapSet` - but that also ignores duplicates, so something like `MapSet.new([3, 4, 3])` isn’t distinguishable from `MapSet.new([3, 4])`.

Another duplicate-friendly approach is to normalize the lists so that only one of `[3,7]` and `[7,3]` appear in the output. For instance, you could sort the values in each list into increasing order.

1 Like

this perhaps?

``````[
[3, 2, 2, 3],
[5, 2, 3],
[3, 4, 3],
[7, 3],
[5, 2, 3],
[3, 5, 2],
[3, 7],
[3, 5, 2]
]
|> Enum.uniq_by(& Enum.sort(&1))

[[3, 2, 2, 3], [5, 2, 3], [3, 4, 3], [7, 3]]
``````
3 Likes

This is a poorly described problem. You talk about the sum of the sublists, but they are all 10.

`Enum.uniq_by(input, &Enum.sum/1)` will remove all but the first element because they all sum to 10.

Trisolaran’s solution of `Enum.uniq_by(input, &Enum.sort/1)` matches what you say you want the output to be, so go with that.

2 Likes

When sum = 0 then possible_subsets is returning
output : [0], []
but Expected output : []
How can i do that??

Can you describe why `[0]` is returned in the output?
If you can understand why it’s returned, you might be able to find a solution yourself.
If you cannot find, please describe what you thought about, tried out,…

4 Likes

I spent lot of time on it but it not fix.

``````iex(12)> list = [3, 5, 2, 7, 4, 2, 3]
[3, 5, 2, 7, 4, 2, 3]
iex(13)> possible_subsets(list, 0)
[[]]
``````

mix test result

`````` 1) test check the expected sum of subsets of matrix for sum as 0 (SumOfSubsetsTest)
test/sum_of_subsets_test.exs:98
Assertion with == failed
code:  assert SumOfSubsets.possible_subsets(list, 0) == expected_possible_subsets_matrix
left:  [[]]
right: []
stacktrace:
test/sum_of_subsets_test.exs:101: (test)

........

Finished in 0.2 seconds (0.00s async, 0.2s sync)
9 tests, 1 failure
``````

You should use MapSet, as @al2o3cr told You

It is a personal assignment, people are unwilling to give You a direct answer, but hints to get You on the right track.

2 Likes

ok i got it why [0] is coming in output but now i don’t understand how to get this as output `[]` not this `[[]]` when i run this possible_subsets(list, 0)??

Your code literally has that as output:

``````defp subsets([]), do: [[]]
``````

So if subsets is called with an empty list. You return a list with one element: an empty list.

3 Likes

yeah…so how to fix it , i tried if & else loop also but it is not working…Can u tell me how to do it??

Look at the function…

What is the return value?

You can do it… just return what You want

3 Likes

Yeah…but if i did this

``````defp subsets([]), do: []
``````

Then this happens

``````iex(8)> sum_of_one(array_of_digits, 0)
[]
iex(9)> sum_of_one(array_of_digits, 10)
[]
``````

Make sure you clearly understand what subsets is doing.
Do you understand what happens if you pass in an empty list, a list with one element, 2 elements, more?

If you understand that and it appears correct to you, then continue to `possible_subsets` and check how it reacts to getting `[[]]` as output from subsets.
If that is still correct but you don’t want to return it you could add an other filter at the end.

I suggest you try to go through what happens in your code on paper and validate if your understanding is correct. You only have a small amount of code so it shouldn’t take much time.

2 Likes

Done Guys, Thankyou so much all of us.

how did this happen please understand

Hello and welcome,

What have You tried so far?

``````def sum_of_all(list, matrix_2d, list_sanitizer \\ &valid_sum/1) do
calculated_integer_sum = list_sanitizer.(matrix_2d) |> Enum.sum()
sum_of_one(list, calculated_integer_sum)
end
``````

I formatted your last post… code sample should be wrapped inside ```as in Markdown