How to remove only adjacent duplicates in an array of integers in Swift?

送分小仙女□ 提交于 2020-01-25 07:30:08

问题


My implementation:

extension Array where Element:Equatable {
    func removeDuplicates() -> [Element] {
        var result = [Element]()

        for value in self {
            if result.contains(value) == false {
                let r = result.append(value)       
            }
        }
        return result
    }
}
let arrayOfInts = [1, 2, 2, 3, 3, 3, 1].reverse()
for element in arrayOfInts.removeDuplicates(){
    print(element)
}

I would like to perform operations on the array after the adjacent integers have been removed.


回答1:


Your extension is defined on array and reversed returns a reversed view into an array so you need to decay into an actual array. See solution below.

extension Array where Element:Equatable {
  func removeConsecutiveDuplicates() -> [Element] {
    var previousElement: Element? = nil
    return reduce(into: []) { total, element in
      defer {
        previousElement = element
      }
      guard previousElement != element else {
        return
      }
      total.append(element)
    }
  }
}
let arrayOfInts = Array([1, 2, 2, 3, 3, 3, 1].reversed())
for element in arrayOfInts.removeConsecutiveDuplicates() {
    print(element)
}

EDIT: I should add to remove consecutive elements its sufficient to just memoize the last element as in my example. If you want all duplicates then use a Set<Element> and not a [Element] as in your example because contains on a set is O(C) but its O(n) on an array.




回答2:


This can be simply solved by adding the elements of the current array to a new array but only appending the current element if it's not equal to the last element to take care of the duplicate adjacent problem. Try this solution.

extension Array where Element:Equatable {
func removeDuplicates()->[Element]{
    guard !self.isEmpty else { return [] }
    var noDuplicates = [Element]()

    for (index, element) in self.enumerated(){
        if index > 0{

            if element != noDuplicates.last{

                noDuplicates.append(element)
                   }
        }
        else{
            noDuplicates.append(element)
        }


    }

    return noDuplicates
}
}

EDIT: Also if you're having trouble with using this extension on the ReversedCollection after you reverse the array it's because Swift recognizes it as being different from an Array type. Once you reverse an Array it no longer is an array it becomes a separate type called ReversedCollection. If you want to apply this extension to ReversedCollection types as well as arrays, you need to create an extension that's not limited to arrays and includes both Reversed Collection and Array types or convert the ReversedCollection into an Array

extension Collection where Element:Equatable {

Or

let arrayOfInts = Array([1, 2, 2, 3, 3, 3, 1].reversed())
for element in arrayOfInts.removeDuplicates(){
    print(element)
}


来源:https://stackoverflow.com/questions/59382540/how-to-remove-only-adjacent-duplicates-in-an-array-of-integers-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!