Remove Adjacent Duplicate Elements from a List

Remove adjacent duplicate elements from a list

Use a generator to iterate over the elements of the list, and yield a new one only when it has changed.

itertools.groupby does exactly this.

You can modify the passed-in list if you iterate over a copy:

for elt in theList[ : ]:
...

How to remove adjacent duplicate elements in a list using list comprehensions?

You could use list comprehension and enumerate with solution suggested by @AChampion:

xs = [1,2,2,2,1,1]
In [115]: [n for i, n in enumerate(xs) if i==0 or n != xs[i-1]]
Out[115]: [1, 2, 1]

That list comprehension return item if it's first or for the following if it's not equal to previous. It'll work due to lazy evaluations of if statement.

Scheme remove adjacent duplicates

Here is how to do it:

If the argument is the empty list or the cdr is the empty list then the result is the argument.

Else if the first and the second element is the same, then skip the first element.

Else cons the first element with the recursion with the cdr or the argument.. eg.

(remove-adjacent-duplicates '(a b)) ; ==
(cons (car '(a b)) (remove-adjacent-duplicates (cdr '(a b))))

Python remove one of two duplicates (next to each other) in a list

Using list comprehension:

[A[i] for i in range(len(A)) if (i==0) or A[i] != A[i-1]]
#[1, 2, 3, 4, 5, 6, 4, 7]

The logic is to iterate through each index in the list and keep the number if either of the following conditions hold:

  • i==0 (the first element)
  • A[i] != A[i-1]

Remove Adjacent Duplicates in string slice

The func removeAdjacentDuplicate takes the slice "as if" it is a reference to tempData

The capacity and length of tempData in the main() stays the same for the lifetime
of the program

In the removeAdjacentDuplicate func each time a dupe is found the final value of "ghi" is moved from the end to the end - 1. So in the memory at the end of the
slice there are repeated "ghi"

When the control returns to the main, the program prints out the now modified
slice tempData. Because it was passed in a similar way to a reference to the
function it is this memory that was modified. The function call did not make a copy of the memory

You can see this behaviour by looking at the cap() and len() as the program runs

package main

import (
"fmt"
)

func main() {
tempData := []string{"abc", "abc", "abc", "def", "def", "ghi"}
removeAdjacentDuplicates(tempData)
fmt.Println(tempData,cap(tempData),len(tempData))
}

func removeAdjacentDuplicates(data []string) {
for j := 1; j < len(data); {
if data[j-1] == data[j] {
data = append(data[:j], data[j+1:]...)
fmt.Println(data,cap(data),len(data))
} else {
j++
}
}
fmt.Println(data, cap(data),len(data))
}

Remove adjacent duplicates on array

Regarding your solution, just add return before remAdjDups(arr...

P.S.

I used Array.prototype.filter for that

const input = [2, 2, 0, 2, 3, 3, 0, 0, 1, 1];
const result = input.filter((i,idx) => input[idx-1] !== i)
console.log(result)

Haskell - Removing adjacent duplicates from a list

Start in last-first order: first remove duplicates from the tail, then check if head of the input equals to head of the tail result (which, by this moment, won't have any duplicates, so the only possible pair is head of the input vs. head of the tail result):

main = mapM_ (print . squeeze) ["acvvca", "1456776541", "abbac", "aabaabckllm"]

squeeze :: Eq a => [a] -> [a]
squeeze (x:xs) = let ys = squeeze xs in case ys of
(y:ys') | x == y -> ys'
_ -> x:ys
squeeze _ = []

Outputs

""
""
"c"
"ckm"


Related Topics



Leave a reply



Submit