Sublist pattern matching
Several demonstrative examples:
list = {{1, 2, 3}, {1, 2, 3}, {1, 2, 2, 3}, {1, 3, 3, 5}};
list /. {p___, {a___, x_, x_, b___}, q___} :> {p, {a, x, b}, q}
(* {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}, {1, 3, 3, 5}} *)
list /. {a___, x_, x_, b___} :> {a, x, b}
(* {{1, 2, 3}, {1, 2, 2, 3}, {1, 3, 3, 5}} *)
Replace[list, {a___, x_, x_, b___} :> {a, x, b}, {1}]
(* {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}, {1, 3, 5}} *)
I think the last one is what you are looking for.
If you want to delete many sequential duplicates with patterns you can use
Replace[list, {a___, Repeated[x_, {2, ∞}], b___} :> {a, x, b}, {1}]
Map ReplaceAll
at Level
1:
# /. {a___, x_, x_, b___} :> {a, x, b} & /@ list
(* {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}, {1, 3, 5}} *)
Map
DeleteDuplicates
at Level
1:
DeleteDuplicates /@ list
(* {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}, {1, 3, 5}} *)
Alternative methods to ReplaceRepeated
to get {{1, 2, 3}, {1, 3, 5}}
:
FixedPoint[# /. {a___, x_, x_, b___} :> {a, x, b} &, list]
(* {{1, 2, 3}, {1, 3, 5}} *)
Map[DeleteDuplicates, list, {0, 1}]
(* {{1, 2, 3}, {1, 3, 5}} *)
And to get {{1, 2, 3}, {1, 2, 2, 3}, {1, 3, 3, 5}}
:
Map[# /. {a___, x_, x_, b___} :> {a, x, b} &, list, {0}]
(* {{1, 2, 3}, {1, 2, 2, 3}, {1, 3, 3, 5}} *)
or
Map[DeleteDuplicates, list, {0}]
(* {{1, 2, 3}, {1, 2, 2, 3}, {1, 3, 3, 5}} *)