Fortran Fortran 90 & elements array comparaison

AI Thread Summary
To efficiently identify the redundant element in a fixed-size array of four elements, one suggested method is to sort the array in numerical order, which allows for easy detection of duplicates due to their consecutive positioning. Another approach involves a brute force comparison of pairs, as only six comparisons are needed among the four elements. This method simplifies the process of finding the duplicate and allows for the construction of a new array with the remaining unique elements.
aihaike
Messages
52
Reaction score
0
Dear all,

I'm trying to figure about an efficient way to to get the redundant element in my array.
For instance, let's take the array [0,2,3,0].
I'd like to get out [2,0,3] (or 3,0,2 it doesn't matter).
So it find out which element is duplicated, an puts its value in between the 2 other elements.
The size of my initial array is always 4 and the final one always 3.
The duplicated element can have any index.
It's look very simple, and it certainly is, but I can not figure.
Some help would be very helpful.
Thanks in advance,

Eric.
 
Technology news on Phys.org
First sort the array in numerical order, using any of a number of sorting methods. Then it's easy to find duplicates because they're in consecutive positions.

(added) Ah, now I see that you always start with four elements. In that case you could even do it by brute force because you need to compare only six pairs of elements:

#1 with #2
#1 with #3
#1 with #4
#2 with #3
#2 with #4
#3 with #4
 
I'll try that, thanks!
 
yes!
you're right, i'll go that way.
Thans again
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top