[Question]
  [
Consider a length-\$n\$ array of positive integers.
* The **complement** of the element with index \$i\$ is the element with index \$n - i - 1\$.
* The **twin** of an element \$a\$ is its complement after removing all elements not equal to \$a\$.
For example, in the array `[1, 2, 3, 4, 2, 1, 3, 2]`, the complement of the first `2` is the last `3`, and the twin of the first `2` is the last `2`. (In general, the twin of the first \$a\$ is the last \$a\$, the twin of the second \$a\$ is the second-to-last \$a\$, etc.)
Given a non-empty array of positive integers, replace each element with the complement of its twin.
```
[1, 2, 3, 4, 2, 1, 3, 2] -> [3, 1, 2, 2, 4, 2, 1, 3]
[1, 2, 1, 3] -> [2, 1, 3, 1]
[10] -> [10]
[8, 3, 1, 8, 1, 8, 3, 10, 3, 8, 10, 8] -> [8, 8, 10, 1, 8, 3, 8, 3, 10, 3, 1, 8]
```
  
[Answer]
# [R](https://www.r-project.org/), ~~65~~ ~~59~~ ~~50~~ ~~41~~ 36 bytes
```
function(x,`!`=order)x[!rev(x)][!!x]
```
[Try it online!](https://tio.run/##dYzBCsMgEETv/YqVXhQsqOnBS/ojIg1YC4HGFDGtf2/R0A2UFGSdnX0zsaT3GK5unp4PP/mQ@nJfgkvjHGjmAxn6Od58ZNmQ6F80M2sIyfY3RR2VHBSHjsO5Cdm0YgyOcLqA6Zql2tsIe/hXVK8Yxj65GxAISrEH6DXLQX9nXUX79Ko0Vmj0EN3oatnyAQ "R ‚Äì Try It Online")
**How?**
First, a couple of tricks used:
1. Re-assign the `!` operator to become the `order` function,
2. Notice that `rank(x)` is equivalent to `order(order(x))`, with tied ranks broken by first occurrence.
So, ungolfed:
```
twin_complement=
function(x){
 o=order(rev(x))        # get the order of reversed values of x, 
                        # breaking ties in reverse order-of-first-occurrence in x;
 t=o[rank(x,            # use this to get the twin index for each index of reversed x
  ties.method="first")] # = the element at the rank position of each x value,
                        # now breaking ties in order-of-first-occurrence in x;
 x[t]                   # finally, return the complement of x 
                        # at each twin position in reversed x.
}
```
[Answer]
# [Minipyth](https://github.com/isaacg1/minipyth), 30 bytes (17.7 bytes if encoded)
```
mhoithzsmqmscbnqgihcmmzsbbnlis
```
Minipyth is a new minimalist language I've designed. It only uses lowercase letters, and it doesn't have things like variables, arity 2 functions, etc.
Because Minipyth only uses lowercase letters, it could be re-encoded to be much shorter. I'm therefore also listing its information-theoretic length, which shorter by a factor of $$\log(26)/\log(256) \approxeq 59\%.$$
Minipyth doesn't have an online executor yet, but it can be used offline through the repository linked above.
It's executed from right to left, in function application order.
I'll break it down into several pieces to show what's going on.
First, we prepare a list consisting of elements of the form [complement, index, value], where value is an element of the input, index is its position in the input, and complement is its complement.
```
cmmzsbbnlis
     b         Bifurcate: make a list by applying two functions to the input
      b        Function 1: Bifurcate on
       n       negate, reversing the input,
        l      length, the length of the input
         is    inverse sum, wrap the input in a list.
    s          sum, concatenate into a single list of 3 elements
 m             map on
  mz           the default map, identify on lists, casts int x to [0, ..., x-1]
c              chop, transpose the three lists to form lists of 3 elements.
```
Next, we group these elements by their values. This is simply `gih`, group by inverse head (last element).
Now, we need to form the twins.
```
smqmscbnq
 m           Map over the groups.
      bn     Bifurcate on n, reversing the group.
     c       Chop, forming pairs.
   ms        Sum the pairs, concatenating into one list
s            Sum, concatenating the lists back into one
```
z and q just make everything parse correctly.
Now, our elements are six element lists: [twin's complement, twin's index, twin's value, complement, index, value].
We just need to put things back into their original order using the index, then extract the twin's complement.
```
 mhoithz
   o         Order by
    i        Inverse of
     t       Tail
      h      Head
             The inverse of tail removes the final element,
             then the inverse of head returns the second-to-last
 mh          Map to head
```
One note, at the time of this writing (commit 4436b81a3), inverse reverses the order of application of its arguments. This was to mimic inverse functions, but I've decided I don't like it, and I will undo that. In the future, the equivalent program will have h and t swapped:
```
mhoithzsmqmscbnqgihcmmzsbbnlis
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 8 bytes
```
ƒ†UF·ª§·ªã·ª§·ªãU
```
[Try it online!](https://tio.run/##y0rNyan8///IglC3h7uXPNzdDSFD////H22ho2Cso2Coo2ABI0FcAzBlAWFZxAIA "Jelly ‚Äì Try It Online")
Port of Jonah's J answer:
```
ƒ†          Group indices
 U         Reverse each group
  F        Flatten
   ·ª§       Grade up
    ·ªã·ª§     Index into graded-up original list
      ·ªãU   Index into reversed list
           e.g. input: ‚ÄúXYXZYY‚Äù (needn't be integers!)
        ƒ†: [[1,3],[2,5,6],[4]]
        U: [[3,1],[6,5,2],[4]]
        F: [ 3,1,  6,5,2,  4 ]
        ·ª§: [2,5,1,6,4,3]
           This is a "rank" of the input, breaking ties backwards:
            X Y X Z Y Y
            2   1          rank Xs backwards
              5     4 3    rank Ys backwards
                  6        rank Zs backwards
         ‚Üí [2,5,1,6,4,3].
           Meanwhile, ·ª§ is [1,3,2,5,6,4] (equiv. to ƒ†F),
           indices that sort the input:
           [X,Y,X,Z,Y,Y]
           [1,  3,         indices of Xs
              2,    5,6,   indices of Ys
                  4]       indices of Zs
           Now ·ªã·ª§ permutes one list by the other,
           mapping each element to its twin's index:
       ·ªã·ª§: [3,6,1,4,5,2]
            X Y X Z Y Y
            3   1          Twin indices of Xs
              6     5 2    Twin indices of Ys
                  4        Twin indices of Zs
           
           And we only have to index into the backwards input:
       ·ªãU: ‚ÄúZXYXYY‚Äù
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jellylanguage), 9 bytes
```
ƒ†UF,·ª§yJ·ªãU
```
[Try It Online!](https://jht.hyper-neutrino.xyz/tio#WyIiLCLEoFVGLOG7pHlK4buLVSIsIiIsIiIsWyJbOCwgMywgMSwgOCwgMSwgOCwgMywgMTAsIDMsIDgsIDEwLCA4XSJdXQ==)
```
ƒ†UF,·ª§yJ·ªãU  Main Link
ƒ†          Group indices by equal elements
 U         Reverse each block
  F        Flatten
   ,·ª§      Pair with the grade of the original list
     yJ    Apply this as a translation to the indices of the list
       ·ªãU  Index that into the reverse of the original list
```
-1 byte thanks to Lynn with the grading up trick. Grading up a list sorts the indices by their corresponding values.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 bytes
```
·ª§·ª§·ªãU·ª§$·ªã
```
[Try it online!](https://tio.run/##y0rNyan8///h7iVg1B0KpFSA9P///6MtdBSMdRQMdRQsYCSIawCmLCAsi1gA "Jelly ‚Äì Try It Online")
Port of [my R answer](https://codegolf.stackexchange.com/a/240053/95126).
Pseudocode:
`x [ order of reverse of x ] [ order of order of x ]`
[Jelly](https://github.com/DennisMitchell/jelly) explanation:
```
·ª§·ª§        # get the order of the order of the input;
  ·ªã       # use this to index into...
     $    # (previous two links together)
   U      # ...reverse of
    ·ª§     # ...order of the input;
      ·ªã   # finally use this to index into the input
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 10 bytes
```
ƒ†≈º·πö$‚Ç¨·∫é·∏¢√û·ªã·πö
```
A full program that accepts the list and prints the result.
**[Try it online!](https://tio.run/##y0rNyan8///IgqN7Hu6cpfKoac3DXX0Pdyw6PO/h7m6gyP///6MNdRSMdBSMdRRMwAxDMNsoFgA "Jelly ‚Äì Try It Online")**
### How?
```
ƒ†≈º·πö$‚Ç¨·∫é·∏¢√û·ªã·πö - Main Link: list, A
ƒ†          - group indices by their element's values
   $‚Ç¨      - for each:
  ·πö        -   reverse that list of indices having equal values
 ≈º         -   zip the two together
     ·∫é     - tighten (to a list of [index, twin index] pairs)
       √û   - sort by:
      ·∏¢    -   pop head from the pair being inspected
         ·πö - reverse A
        ·ªã  - use the (remaining) twin indices to index into the reversed copy of A
           - implicit, smashing print
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~18~~ 17 bytes
```
Ôº©Ôº•Œ∏¬ßŒ∏¬ß‚åïÔº°‚ÆåŒ∏Œπ‚åï‚åïÔº°Œ∏ŒπŒ∫
```
[Try it online!](https://tio.run/##S85ILErOT8z5/z@gKDOvRMM5sbhEwzexQKNQR8GxxDMvJbUCmemWmZfimJOjEZRallpUnKpRqKmjkAnEIHG4ZCFELFsTDKz//4@OttBRMNZRMNRRsICRIK4BmLKAsCxiY//rluUAAA "Charcoal ‚Äì Try It Online") Link is to verbose version of code. Explanation:
```
  Œ∏                 Input array
 Ôº•                  Map over elements
    Œ∏               Input array
   ¬ß                Indexed by
         Œ∏          Input array
        ‚Æå           Reversed
      ‚åïÔº°            Find all occurrences of
          Œπ         Current value
     ¬ß              Indexed by
              Œ∏     Input array
            ‚åïÔº°      Find all occurrences of
               Œπ    Current value
           ‚åï        Find index of
                Œ∫   Current index
Ôº©                   Cast to string
                    Implicitly print
```
[Answer]
# [BQN](https://mlochbaum.github.io/BQN/), 9 bytes[SBCS](https://github.com/mlochbaum/BQN/blob/master/commentary/sbcs.bqn)
Port of [Dominic van Essen's R answer](https://codegolf.stackexchange.com/a/240053/64121).
```
‚çã‚àò‚çã‚äè‚çã‚àò‚åΩ‚äè‚ä¢
```
[Run online!](https://mlochbaum.github.io/BQN/try.html#code=RiDihpAg4o2L4oiY4o2L4oqP4o2L4oiY4oy94oqP4oqiCgo+KOKLiOKfnEYpwqgg4p+oMeKAvzLigL8z4oC/NOKAvzLigL8x4oC/M+KAvzIKMeKAvzLigL8x4oC/Mwrii4gxMAo44oC/M+KAvzHigL844oC/MeKAvzjigL8z4oC/MTDigL8z4oC/OOKAvzEw4oC/OOKfqQ==)
---
# [BQN](https://mlochbaum.github.io/BQN/), ~~17~~ 13 bytes[SBCS](https://github.com/mlochbaum/BQN/blob/master/commentary/sbcs.bqn)
~~My own attempt that ended up being similar to~~ Based on Lynn's Jelly answer that is a port of [Jonah's J answer](https://codegolf.stackexchange.com/a/240062/64121).
```
‚åΩ‚äèÀú{‚çã‚àæ‚åΩ¬®‚äîùï©}‚äè‚çã
```
[Run online!](https://mlochbaum.github.io/BQN/try.html#code=RiDihpAg4oy94oqPy5x74o2L4oi+4oy9wqjiipTwnZWpfeKKj+KNiwoKPijii4jin5xGKcKoIOKfqDHigL8y4oC/M+KAvzTigL8y4oC/MeKAvzPigL8yCjHigL8y4oC/MeKAvzMK4ouIMTAKOOKAvzPigL8x4oC/OOKAvzHigL844oC/M+KAvzEw4oC/M+KAvzjigL8xMOKAvzjin6k=)
[Answer]
# [R](https://www.r-project.org/), ~~41~~ ~~39~~ 32 bytes
```
function(x)ave(rev(x),x,FUN=rev)
```
[Try it online!](https://tio.run/##K/pfUp6ZF5@cn1uQk5qbmldi@z@tNC@5JDM/T6NCM7EsVaMotQzI0qnQcQv1swVyNNF1aCRrGOooGOkoGOsomIAZhmC2kaamgrKCrp1CtDFYyAiMECpiuXAZBJKFa4abZ4hVgwFcoaEBNgUWEL06ChYwEsQ1AFMWEJYF3AgLuBhcKUI1SCj2PwA "R ‚Äì Try It Online")
I thought of this as a `split %>% lapply %>% unsplit` before remembering that `ave` does the same thing for fewer bytes.
Test harness taken from [Dominic van Essen's answer.](https://codegolf.stackexchange.com/a/240053/67312)
Explanation to follow.
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 19 bytes
```
∆õ?=T·∏ÇZ;U√ûf¬µh;vt?·πò$ƒ∞
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCLGmz89VOG4glo7VcOeZsK1aDt2dD/huZgkxLAiLCIiLCJbMSwgMiwgMywgNCwgMiwgMSwgMywgMl0iXQ==)
A clumsy port of Jonathan Allan's answer.
```
∆õ     ;             # Map input to...
 ?=T                # Indices of that element
    ·∏ÇZ              # Zipped with its reverse
       U√ûf          # Uniquify and flatten to a list of [index, pair index] pairs
          ¬µh;       # Sort by first item
             vt     # Get second item of each
                  ƒ∞ # Use those to index into
               ?·πò$  # Input reversed
```
[Answer]
# [J](http://jsoftware.com/), 24 bytes
```
|.{~[:(/:&;~|.&.>)<@I.@=
```
[Try it online!](https://tio.run/##VY/BCsIwEETv/YrBQ7UQY2M8hGilKAiCqHj1VGu0CDbSVhEUf70mNohCdnfe7mxCTnWLtg@IJNogCCFNdCmmm8WsftLHays7PekPX0/q03Ewiuc0jurAU2mm0ekTMAJucoCuxMGS6zkHJ65nzuA7/HPz34kRbpNIFjqflZ63nFCo@0WlldpDX6vLtUKaJfnR4C7Fao1SqXOJSiNLbgpJDlUUuqDuQgEBFoKZwj9hgFt0r4jmJ8SKJlsMP0U0StRv "J ‚Äì Try It Online")
[Answer]
# Python/NumPy, 60 bytes
```
def f(a):
 b=a*0
 for S in a:b[a==S]=a[a[::-1]==S]
 return b
```
[Attempt This Online!](https://ato.pxeger.com/run?1=bU7BSsNAFLznKx457cqmJKmHEFzBFqFisYcWLKw5bDChC8lm2W6UIP0SL7koePJ7-jcmmxopCMu-mXnzhnn_VI3ZVbJtP2qTe9Hx6jnLIUccxw6klF_4DuSVhjUICTxOGad0nVDOOItjL0h65oDOTK0lpKeML1GqShuQdaka4HuQynndiSKDoEsFQSqgUHKFshdeECFVbRCe7FUhDHLBuwYX486ntJAGCZIjqSZca94ggTFxnx7eZjfL5WoDs9UWHu82C9ge_hHni9v5_cFl_XVRoPMYSiuc4KFwe_xmAYGQwJTApQWBxWHSt2FTy0P7_taJczqyxBrHw6Bf-oPYTYdFg0wg-v176tsRDSga7NEojL4zd68mQ-8f)
Expects and returns numpy arrays.
[Answer]
# JavaScript (ES6), 56 bytes
```
a=>a.map(v=>a[b[j=b.indexOf(v)]=0,j],b=[...a].reverse())
```
[Try it online!](https://tio.run/##hY1NDoMgFIT3PQVLSCj@dcEGr9ADEBao2GisGG1Ib0/l2dpWm5QQGIaZ77Xa6akcm@F27G1lfC28FrlmVz1gNwtZyFYUrOkrcz/X2BElYtoqWgjJGNOKjcaZcTKYEF/afrKdYZ294BrLhKKUooyiE4gEdKoIQZsVRUhmEEhhv/Pq8JMJX3vOlrkOTfac@E//kzOHt32@cCnirzM8Y7j4ojhMCP3gPM01@9UILlL@AQ "JavaScript (Node.js) ‚Äì Try It Online")
### Commented
```
a =>                // a[] = input array
a.map(v =>          // for each value v in a[]:
  a[                //   lookup in a[]:
    b[ j =          //     get the position j
       b.indexOf(v) //     of the 1st occurrence of v in b[]
    ] = 0,          //     and clear it
    j               //     get a[j]
  ],                //   end of lookup
  b = [...a]        //   start with b[] initialized to
      .reverse()    //   a copy of a[] in reverse order
)                   // end of map()
```
[Answer]
# JavaScript (ES2022), 48 bytes
```
a=>a.map(g=n=>a.at(g[n]=~-g[n])-n?g(n):a[~g[n]])
```
```
f=
a=>a.map(g=n=>a.at(g[n]=~-g[n])-n?g(n):a[~g[n]])
console.log(f([1, 2, 3, 4, 2, 1, 3, 2]) + ''); // [3, 1, 2, 2, 4, 2, 1, 3]
console.log(f([1, 2, 1, 3]) + ''); // [2, 1, 3, 1]
console.log(f([10]) + ''); // [10]
console.log(f([8, 3, 1, 8, 1, 8, 3, 10, 3, 8, 10, 8]) + ''); // [8, 8, 10, 1, 8, 3, 8, 3, 10, 3, 1, 8]
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 18 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
√ôvIIyQ∆∂0K¬©RIgŒ±√®¬Æ<«ù
```
[Try it online](https://tio.run/##yy9OTMpM/f//8MwyT8/KwGPbDLwPrQzyTD@38fCKQ@tsjs/9/z/aQsdYx1DHAoyBLAMgYQGiLGIB) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfeXhCaH/D88si4ioDDy2zcD70MqgiPRzGw@vOLTO5vjc/7U6/6OjDXWMdIx1TICkIZA2itVRAAsBOWCmAYi0AMoY6liAMZBlACQsQJRFbCwA).
**Explanation:**
```
√ô                   # Uniquify the (implicit) input-list
 v                  # Loop over each unique value `y`:
   IyQ              #  Check for each value in the input-list if it's equal to `y`
                    #  (1 if truthy; 0 if falsey)
      ∆∂             #  Multiply each by its 1-based index
       0K           #  Remove all 0s
         ¬©          #  Store this list in variable `¬Æ` (without popping)
          R         #  Reverse it
             Œ±      #  Get the absolute difference between each 1-based index
           Ig       #  and the input-length
  I           √®     #  Use that to (0-based) index into the input-list
               ¬Æ<   #  Push `¬Æ` and decrease each by 1 to make them 0-based
                 «ù  #  Insert the values at those indices into the list
                    #  (which will be the implicit input-list in the first iteration)
                    # (after the loop, the result is output implicitly)
```
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~131~~ 127 bytes
```
c;j;i;*w;*f(a,n)int*a;{for(w=calloc(i=n,4);i--;w[i]=a[n+~j]){for(j=c=0;j<=i;)c+=a[i]==a[j++];for(j=n;c;)c-=a[i]==a[--j];}a=w;};
```
[Try it online!](https://tio.run/##nVLbitswEH3frxCGLL7INL4spJ1V@1D6FY4pRpFbX@oEy5DQ4P56OpZ8kd1CywZHGs2Z0Zw5I@5/4/zx4FBCAe4V3NzOaOMUTedmcM/PrX1lPKvrM7cL1tDYgcL34ZoUKcuSxvtVpo6KKhlneyhfWQEO9xDDAFxLz0tB4w1whPwZ8v0yhT5jV@jhgfXIj6xobIfcnwj@BkcnZBckKWHkHlASUhJREisjUHbYwxwXmnEDbGDRiO0NX6x9B3URxh@mdTju1XbQ1qGHFaOvtU6VxU9xzm1F0nk3nlx9pMRAwzUartFojUZrNF6jsbOQcRUbqcmoslQrobdIb7HBXtwugnfiNGqqGw/VF291m2JHXWfNgw2@1XbyL/qOMs7qrjQOVvq6c7rOngnThc9iRos5dcnPjewI/561Lq6CV6LVN1nH25fweHv/Gf8vFiXmObLGbHynxB6aKJqTuGHaHkbzdZrJVHAZy@wB4nkqenrCy5zwKj0rhadgwqQeUXxXW/jSYkBuW4nlLM6ZZaUZVsiuHopXZmEzfSd3J@y5@jQ0/sHCtUuq1Liy/6NiSvyPZFVWdSKxJD5MSuotIhCZh/e3Ni@ZHLKDNzWic9X2/EwEsieMEYk7/GfH8t8d7@Sxwcjp3QzVzJS8FcKWo6N/6h@/AQ "C (gcc) ‚Äì Try It Online")
*Saved 4 bytes thanks to [ceilingcat](https://codegolf.stackexchange.com/users/52904/ceilingcat)!!!*
Inputs a pointer to an array (\$a\$) of integers and its length (because pointers in C carry no length info).  
Returns a pointer to an array of all of \$a\$'s elements' twins' complements.
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 6 bytes
```
‚áß‚Çå·πò‚áßƒ∞ƒ∞
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCLih6figozhuZjih6fEsMSwIiwiIiwiWzgsIDMsIDEsIDgsIDEsIDgsIDMsIDEwLCAzLCA4LCAxMCwgOF0iXQ==)
Port of [Dominic's Jelly answer](https://codegolf.stackexchange.com/a/240159/100664).
```
‚áß      # Gradeup
 ‚Çå·πò‚áß   # Push ^ reversed, ^ gradeup'd
    ƒ∞  # Use the reversed copy to index into the double-gradeup'd
     ƒ∞ # Use those indicies to index into the input.
```
]