10
$\begingroup$

My code replaces each repeated element by "X".

ReplaceRepeated[{1, 2, 3, 2, 5, 2, 1, 5, 5, 2, 3, 3, 
    7}, {a1___, y_ /; y != #, a2___, y_, a3___} :> {a1, y, a2, #, 
     a3}] &@"X"

(*{1, 2, 3, "X", 5, "X", "X", "X", "X", "X", "X", "X", 7}*)

What is the most elegant code to do this?

$\endgroup$

4 Answers 4

19
$\begingroup$
ClearAll[replaceDuplicates]
replaceDuplicates[rep_: "X"] := Module[{f}, f[y_] := (f[y] = rep; y); f /@ #] &

Examples:

replaceDuplicates[] @ {1, 2, 3, 2, 5, 2, 1, 5, 5, 2, 3, 3, 7}
{1, 2, 3, "X", 5, "X", "X", "X", "X", "X", "X", "X", 7}
replaceDuplicates[Nothing] @ {1, 2, 3, 2, 5, 2, 1, 5, 5, 2, 3, 3, 7}
{1, 2, 3, 5, 7}

Alternatively:

ClearAll[replaceDuplicates2]
replaceDuplicates2[rep_: "X"] := Module[{y = #}, 
   y[[Join @@ (Rest /@ Values @ PositionIndex[y])]] = rep; y] &

Examples:

replaceDuplicates2[] @ {1, 2, 3, 2, 5, 2, 1, 5, 5, 2, 3, 3, 7}
{1, 2, 3, "X", 5, "X", "X", "X", "X", "X", "X", "X", 7}
replaceDuplicates2[Nothing] @ {1, 2, 3, 2, 5, 2, 1, 5, 5, 2, 3, 3, 7}
{1, 2, 3, 5, 7}
$\endgroup$
7
  • 7
    $\begingroup$ Very clever, and ten times faster than what I suggested. Cheers! $\endgroup$
    – Roman
    Apr 24, 2021 at 17:30
  • $\begingroup$ I am trying to understand what your function is really doing. $\endgroup$ Apr 24, 2021 at 20:03
  • 2
    $\begingroup$ @azerbajdzan the heart of it is this definition f[y_] := (f[y] = rep; y) which will execute the first time it sees a value y and first perform the assignment f[y] = rep and then return y. That is why the values do not get replaced with "X" the first time they are seen. On the next time the value y is seen, because of the assignment f[y] = rep, the function simply returns y. All of this is wrapped in Module so it's cleaned up properly and then the function is mapped over the list of arguments. $\endgroup$
    – b3m2a1
    Apr 25, 2021 at 2:54
  • 2
    $\begingroup$ +1. One nitpick is that one would need to call Clear[f] (but not ClearAll!) at the end before returning the result, otherwise f will not be garbage-collected and will hang around indefinitely after the call to replaceDuplicates - and if you call it multiple times, then just as many different fs will be left hanging around. $\endgroup$ Apr 26, 2021 at 11:56
  • 1
    $\begingroup$ Thank you @Leonid. Excellent point. $\endgroup$
    – kglr
    Apr 26, 2021 at 12:04
11
$\begingroup$
FoldPairList[If[MemberQ[#1, #2], {"X", #1}, {#2, Append[##]}] &,
             {},
             {1, 2, 3, 2, 5, 2, 1, 5, 5, 2, 3, 3, 7}]

(*    {1, 2, 3, "X", 5, "X", "X", "X", "X", "X", "X", "X", 7}    *)

FoldPairList accumulates a list of "already seen" numbers, starting with the empty list. If the new number is in the "already seen" list, we emit "X" and keep the list unchanged; otherwise, we emit the new number and add it to the "already seen" list (with Append[##] being an abbreviation for Append[#1, #2]).

$\endgroup$
0
8
$\begingroup$

You may use PositionIndex with ReplacePart.

With

x = {1, 2, 3, 2, 5, 2, 1, 5, 5, 2, 3, 3, 7};

and

replaceDups[expr_, rep_ : "X"] := 
 ReplacePart[expr, List /@ Flatten[Rest /@ Values@PositionIndex@expr] -> rep]

Then

replaceDups[x]
{1, 2, 3, "X", 5, "X", "X", "X", "X", "X", "X", "X", 7}

Is also faster than OP solution.

Hope this helps.

$\endgroup$
1
$\begingroup$

Not that elegant but rather straight.

A = {1, 2, 3, 2, 5, 2, 1, 5, 5, 2, 3, 3, 7}

ReplacePart[A, {#} & /@ 
   Select[Range[Length[A]], MemberQ[Take[A, # - 1], A[[#]]] &] -> "X"]

Note that

Select[Range[Length[A]], MemberQ[Take[A, # - 1], A[[#]]] &]

is a set of indices those are duplicated.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.