Skip to main content
deleted 102 characters in body
Source Link
user184635
user184635

This method is an attempt at removing duplicates from arrays with scalability in mind in Swift 4 without the use of extensions. I am asking for any suggestions, criticisms, and/or observations.

func arrayWithDuplicatesRemoved<T: Equatable>(from array: [T]) -> [T] {
    var results = [T]()
    return array.compactMap { (element) -> T? in
        if results.contains(element) {
            return nil
        } else {
            results.append(element)
            return element
        }
    }
}

let dirty = ["apple", "kiwi", "grapefruit", "kiwi", "kiwi", "strawberry", "watermelon", "apple", "banana"]
let clean = arrayWithDuplicatesRemoved(from: dirty)
print(clean) // ["apple", "kiwi", "grapes", "strawberry", "watermelon", "banana"]

This method is an attempt at removing duplicates from arrays with scalability in mind in Swift 4 without the use of extensions. I am asking for any suggestions, criticisms, and/or observations.

func arrayWithDuplicatesRemoved<T: Equatable>(from array: [T]) -> [T] {
    var results = [T]()
    return array.compactMap { (element) -> T? in
        if results.contains(element) {
            return nil
        } else {
            results.append(element)
            return element
        }
    }
}

let dirty = ["apple", "kiwi", "grapefruit", "kiwi", "kiwi", "strawberry", "watermelon", "apple", "banana"]
let clean = arrayWithDuplicatesRemoved(from: dirty)
print(clean) // ["apple", "kiwi", "grapes", "strawberry", "watermelon", "banana"]

This method is an attempt at removing duplicates from arrays without the use of extensions.

func arrayWithDuplicatesRemoved<T: Equatable>(from array: [T]) -> [T] {
    var results = [T]()
    return array.compactMap { (element) -> T? in
        if results.contains(element) {
            return nil
        } else {
            results.append(element)
            return element
        }
    }
}

let dirty = ["apple", "kiwi", "grapefruit", "kiwi", "kiwi", "strawberry", "watermelon", "apple", "banana"]
let clean = arrayWithDuplicatesRemoved(from: dirty)
print(clean) // ["apple", "kiwi", "grapes", "strawberry", "watermelon", "banana"]
Rollback to Revision 2
Source Link

This method is an attempt at removing duplicates from arrays with scalability in mind in Swift 4 without the use of extensions. I am asking for any suggestions, criticisms, and/or observations.

Original: Not the most scalable, nesting like this means the square of the size of the argument is how performance is measured (thanks to Daniel T for pointing that out)

func arrayWithDuplicatesRemoved<T: Equatable>(from array: [T]) -> [T] {
    var results = [T]()
    return array.compactMap { (element) -> T? in
        if results.contains(element) {
            return nil
        } else {
            results.append(element)
            return element
        }
    }
}

Optimized: Much more scalable as performance is not entirely linear but much closer than before

func ArrayWithDuplicatesRemovedFrom<T: Hashable>(array: [T]) -> [T] {
    return Array(Set(array))
}

Run

let dirty = ["apple", "kiwi", "grapefruit", "kiwi", "kiwi", "strawberry", "watermelon", "apple", "banana"]
let clean = ArrayWithDuplicatesRemovedFromarrayWithDuplicatesRemoved(arrayfrom: dirty)
print(clean) // ["kiwi"["apple", "apple""kiwi", "watermelon""grapes", "grapefruit""strawberry", "strawberry""watermelon", "banana"]

This method is an attempt at removing duplicates from arrays with scalability in mind in Swift 4 without the use of extensions. I am asking for any suggestions, criticisms, and/or observations.

Original: Not the most scalable, nesting like this means the square of the size of the argument is how performance is measured (thanks to Daniel T for pointing that out)

func arrayWithDuplicatesRemoved<T: Equatable>(from array: [T]) -> [T] {
    var results = [T]()
    return array.compactMap { (element) -> T? in
        if results.contains(element) {
            return nil
        } else {
            results.append(element)
            return element
        }
    }
}

Optimized: Much more scalable as performance is not entirely linear but much closer than before

func ArrayWithDuplicatesRemovedFrom<T: Hashable>(array: [T]) -> [T] {
    return Array(Set(array))
}

Run

let dirty = ["apple", "kiwi", "grapefruit", "kiwi", "kiwi", "strawberry", "watermelon", "apple", "banana"]
let clean = ArrayWithDuplicatesRemovedFrom(array: dirty)
print(clean) // ["kiwi", "apple", "watermelon", "grapefruit", "strawberry", "banana"]

This method is an attempt at removing duplicates from arrays with scalability in mind in Swift 4 without the use of extensions. I am asking for any suggestions, criticisms, and/or observations.

func arrayWithDuplicatesRemoved<T: Equatable>(from array: [T]) -> [T] {
    var results = [T]()
    return array.compactMap { (element) -> T? in
        if results.contains(element) {
            return nil
        } else {
            results.append(element)
            return element
        }
    }
}

let dirty = ["apple", "kiwi", "grapefruit", "kiwi", "kiwi", "strawberry", "watermelon", "apple", "banana"]
let clean = arrayWithDuplicatesRemoved(from: dirty)
print(clean) // ["apple", "kiwi", "grapes", "strawberry", "watermelon", "banana"]
added 422 characters in body
Source Link
user184635
user184635

This method is an attempt at removing duplicates from arrays with scalability in mind in Swift 4 without the use of extensions. I am asking for any suggestions, criticisms, and/or observations.

Original: Not the most scalable, nesting like this means the square of the size of the argument is how performance is measured (thanks to Daniel T for pointing that out)

func arrayWithDuplicatesRemoved<T: Equatable>(from array: [T]) -> [T] {
    var results = [T]()
    return array.compactMap { (element) -> T? in
        if results.contains(element) {
            return nil
        } else {
            results.append(element)
            return element
        }
    }
}

Optimized: Much more scalable as performance is not entirely linear but much closer than before

func ArrayWithDuplicatesRemovedFrom<T: Hashable>(array: [T]) -> [T] {
    return Array(Set(array))
}

Run

let dirty = ["apple", "kiwi", "grapefruit", "kiwi", "kiwi", "strawberry", "watermelon", "apple", "banana"]
let clean = arrayWithDuplicatesRemovedArrayWithDuplicatesRemovedFrom(fromarray: dirty)
print(clean) // ["apple"["kiwi", "kiwi""apple", "grapes""watermelon", "strawberry""grapefruit", "watermelon""strawberry", "banana"]

This method is an attempt at removing duplicates from arrays with scalability in mind in Swift 4 without the use of extensions. I am asking for any suggestions, criticisms, and/or observations.

func arrayWithDuplicatesRemoved<T: Equatable>(from array: [T]) -> [T] {
    var results = [T]()
    return array.compactMap { (element) -> T? in
        if results.contains(element) {
            return nil
        } else {
            results.append(element)
            return element
        }
    }
}

let dirty = ["apple", "kiwi", "grapefruit", "kiwi", "kiwi", "strawberry", "watermelon", "apple", "banana"]
let clean = arrayWithDuplicatesRemoved(from: dirty)
print(clean) // ["apple", "kiwi", "grapes", "strawberry", "watermelon", "banana"]

This method is an attempt at removing duplicates from arrays with scalability in mind in Swift 4 without the use of extensions. I am asking for any suggestions, criticisms, and/or observations.

Original: Not the most scalable, nesting like this means the square of the size of the argument is how performance is measured (thanks to Daniel T for pointing that out)

func arrayWithDuplicatesRemoved<T: Equatable>(from array: [T]) -> [T] {
    var results = [T]()
    return array.compactMap { (element) -> T? in
        if results.contains(element) {
            return nil
        } else {
            results.append(element)
            return element
        }
    }
}

Optimized: Much more scalable as performance is not entirely linear but much closer than before

func ArrayWithDuplicatesRemovedFrom<T: Hashable>(array: [T]) -> [T] {
    return Array(Set(array))
}

Run

let dirty = ["apple", "kiwi", "grapefruit", "kiwi", "kiwi", "strawberry", "watermelon", "apple", "banana"]
let clean = ArrayWithDuplicatesRemovedFrom(array: dirty)
print(clean) // ["kiwi", "apple", "watermelon", "grapefruit", "strawberry", "banana"]
edited title
Link
user184635
user184635
Loading
Source Link
user184635
user184635
Loading