Question: I'm looking to make a method which detects if the following value in an array is a duplicate, and deletes it if so. It should work for both strings and integers.

For example, given the Array: 

```
arr = ["A", "B", "B", "C", "c", "A", "D", "D"]

```

Return:

```
arr = ["A", "B", "C", "c", "A", "D"]

```

I tried creating an empty Array `a`, and shovelling the values in, providing the following value was not equal to the current one. I attempted this like so:

```
arr.each do |x|
  following_value = arr.index(x) + 1
  a << x unless x == arr[following_value]
end

```

Unfortunately, instead of shovelling one of the duplicate values into the array, it shovelled neither.

```
arr = ["A", "C", "c", "A"] 

```

Can anybody help? Bonus points for telling me exactly what went wrong with my method.

Thanks!

Answer: 
First at all, here is simpler solution:

```
> arr.delete_if.with_index { |e, ind| e == arr[ind+1] }
#=> ["A", "B", "C", "c", "A", "D"]

```

But, this solution will mutate `arr`.
Here are one-line solutions without mutates:

```
arr.each_with_index.with_object([]) { |(e, ind), res| res << e if e != arr[ind+1] }
arr.each_with_object([]) { |e, res| res << e if res.last != e }

```

Your problem in this line: `a << x unless x == arr[following_value]`
You say: put this `element` into `result` if `next element` isn't equal to it. So, instead, you can say something like: *put this element to result if the last element of the result isn't equal to it*:

```
arr.each do |x|
  a << x unless a.last == x
end

```
Derived from [this](https://stackoverflow.com/a/44788219/5101493 "How do I find the first two consecutive elements in my array of numbers?") answer by [Cary Swoveland](https://stackoverflow.com/users/256970, "The Legend"):

```
def remove_consecs ar
  enum = ar.each
  loop.with_object([]) do |_, arr|
    curr = enum.next
    nxt = arr.last || enum.peek
    arr << curr if curr != nxt
  end
end

remove_consecs ["A", "B", "B", 'D', "C", "c", "A", "D", "D"]
#=> ["A", "B", "D", "C", "c", "A", "D"]

```