O
O
Oleg Abrazhaev2014-08-26 11:30:49
Algorithms
Oleg Abrazhaev, 2014-08-26 11:30:49

What algorithm to use to traverse an array and compare each element with the rest in that array?

In general, the essence of the problem is in the title.
An array of size N is given. It is completely filled. There are elements in the array that are considered the same under some conditions.
It is necessary to bypass this array, comparing each with each element along the way and identify (or remove, or mark) elements that are the same.
Now this problem is being solved head-on, i.e.

foreach(array as key1 => value1) {
   //что-то делается

   foreach (array as key2 => value2) {
     //cравнение value1 и value2
   }
}

The applied approach is slow because arrays can be large in size.
The structure of the elements themselves and the comparison conditions is also not trivial.
The implementation language is not important.
----
I would like to clarify that the elements are complex, i.e. they are not comparable up and down.
For example, these are also arrays or objects, and you can only say about them whether they are equal or not according to some conditions (data from their structure).

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene, 2014-08-26
@seyfer

Isn't it possible to sort them ( O(logN) ) according to the given attribute, so that the same elements are next to each other, and then in one pass ( O(N) ) mark all the repeating ones?

D
Dmitry, 2014-08-26
@dmtrrr

map reduce ?

D
Dima Petruk, 2014-08-26
@bavaria

Recording in the system of disjoint sets can help to select?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question