STerrier
STerrier

Reputation: 4015

How to add a unique value to differentiate between duplicate data in a Swift array?

Could someone point me in the right direction on how to solve this issue, please?

I am creating table cells with the values from the structure below. The cells are created and the data is displayed in the cells by the time they were created which works fine.

The issue is some of the cells have the same name and I have an individual id for each cell from the struc Data but I need the user to know which one of the duplicates was created first within the duplicates. Kind of like a sub-number.

For example: 1:apple -1 , 2:pear -1, 3:apple -2

1(position in all the cell) - Apple (name of the cell) - 1 (value based on how many cells are named apple)

The func idName() I created tells us how many occurrences of a name happens but how could I break this down so the data would display like above?

 struct Data {
    var id: Int
     var name: String
    var color: String
    var time: TimeInterval
    var sessionId: Int
    var userId: Int
}

func idName () {
        let idElement = elements //another variable is used to grab the Array
        var counts: [String: Int] = [:]
        var idValue: Int

        for id in idElement {

            counts[id.name] = (counts[id.name] ?? 0) + 1
            for (key, value) in counts {
                print("\(key) occurs \(value) time(s)")

            }
        }
    }

Upvotes: 2

Views: 937

Answers (2)

shivi_shub
shivi_shub

Reputation: 1058

I Just added nameCounter property in Data which will indicate the occurrence of particular name. Like this -

struct Data1 {
    var id: Int
    var name: String
    var nameCOunter: Int? = 1

    init(id: Int, name: String) {
        self.id = id
        self.name = name
    }

    static func addTestData() ->[Data1] {
        var arr = [Data1]()
        let model = Data1(id: 1, name: "apple")
        let model1 = Data1(id: 2, name: "peer")
        let model2 = Data1(id: 3, name: "apple")
        let model3 = Data1(id: 4, name: "orange")
        let model4 = Data1(id: 5, name: "grape")
        let model5 = Data1(id: 6, name: "peer")
        let model6 = Data1(id: 7, name: "apple")

        arr = [model,model1,model2,model3,model4,model5,model6]
        return arr
    }
}

func idName() {
    let idElement = Data1.addTestData()
    var countedElement = [Data1]()
    var nameArr = [String]()
    for var dataModel in idElement {
        nameArr.append(dataModel.name)
        let count = nameArr.filter{$0 == dataModel.name}.count
        dataModel.nameCOunter = count
        countedElement.append(dataModel)
    }
    print(countedElement)
}

Upvotes: 1

RLoniello
RLoniello

Reputation: 2329

"I need the user to know which one of the duplicates was created first."

How a bout adding a date to each item when it is created?

var date: Date = Date()

So you can sort them...

myObjects = myObjects.sorted(by: {
    $0.date.compare($1.date) == .orderedDescending
})

Another way is to add a UUID, this will give you a unique Identifier to reference:

var uuid: UUID = UUID()

var someObjectID = myObject.uuid.uuidString

Update:

When an Element (your data struct) is created, you should be checking your array of elements prior to adding one of the same name, if one of the same name exists then you can increment a counter not stored as a property of the struct.

You can use map and Dictionary(_:uniquingKeysWith:).

to return an array of mapped elements (an array of your data structs).

let mappedElements = elements.map($0.name, 1)

then, count duplicates and create a dictionary containing the number of matching items.

let counts = Dictionary(mappedElements, uniquingKeysWith: +)

this will result in ["apple": 3, "pear": 2, "peach": 1] etc.

Upvotes: 2

Related Questions