A
A
Alexander F2020-05-28 17:45:47
Mathematics
Alexander F, 2020-05-28 17:45:47

How to merge arrays with duplicates into unique lists?

There are many arrays that have duplicate elements: [a,b], [c,d], [a, e], [e,f], [d,h], [h,o], [k,h ]

It is necessary to combine them in such a way that there are no duplicates in the result, for this example it will turn out:
[a, b, e, f], [c, d, h, o, k]. Those. [a, b] and [a, e] are combined into [a, b, e], then there is an array with elements [e, f], as a result, [a, b, e, f] is obtained.

The number of elements in arrays and the number of arrays themselves can be anything. (within reasonable limits, let it be up to 10pcs)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Wataru, 2020-05-31
@Filex

I really don't understand why you can't just combine all the arrays into one (it will just be a list of unique elements). Perhaps you need the maximum number of arrays.
This problem has two solutions - through a graph traversal or through a system of disjoint sets . But they are both essentially the same, just different implementations.
The point is to build a graph where all the different elements of all arrays will be vertices. There are edges between two vertices if the corresponding elements are present in the same input array.
The answer to your problem is the connected components of this graph. The variant through the graph is as follows - for each array, iterate over all pairs of elements in two cycles and add an edge to the graph. Use some sort of associative array of lists to store the adjacency list. Or use an associative array to assign each unique element a unique ordinal number. The adjacency list or incidence matrix can then be stored simply by numbers, as an array of lists, or as a two-dimensional array. Then search in depth or width to find all connected components.
A slightly simpler and faster solution will work through the system of disjoint sets (DSU). First iterate through all the arrays and, as in the first solution, renumber all unique elements. Get DSU, where each element is a set for itself.
Then go through all the elements of each input array and combine its set with the set of the previous element in the same array. Along the way, you can count the number of unions that have been performed (the sets were different before the union_sets operation was performed). So you can count how many arrays will be in the answer. As a result, all elements for each array in the response will be in the same set. Now, to display the answer, get an array of lists with a known number of elements. Use an associative array to assign each set to one of the lists in the response. Go through all the elements and add them to the appropriate list.

G
Griboks, 2020-05-28
@Griboks

A very strange question. These are 2 questions:
1) how to merge arrays
2) how to make arrays unique
If you have an example, then you already know answer #1, then answer #2 will be: remove duplicate arrays. If you don't know answer #1, where did you get the example from? Asking the right question is half the answer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question