Swift 3 For循环比较和更改

时间:2016-12-13 23:38:09

标签: ios arrays swift swift3

到目前为止,这是我的代码

var counter = 0
    for i in 0...9 {
        var val = NamePicker()

        // array to find duplicates
        var buttonValues = ["", "", "", "", "", "", "", "", "", ""] // array for button names
        buttonValues.insert(val, at: counter)
        print(buttonValues[counter])
        counter += 1
    }

此代码将10个字符串值放入我的数组中。我想要做的是找到一种方法来检查我的数组中的每个值。例如,如果我的最终结果数组是[“a”,“a”,“a”,“b”,“b”,“c”,“c”,“e”,“f”,“c”] I想看看是否有同名的三重奏(单个和重复都没问题)。但是,如果有三元组,我想将第3个值从NamePicker()函数更改为另一个val。

所以我的数组

["a","a","a","b","b","c","c","e","f","c"]

有3个“a”和3个“c”,其中两个相同即可,我想将第3个更改为新值,如果新值再生成三个,它将会改变,直到没有更多三元组。

以便数组可能具有

的最终结果
["a","a","f","b","b","c","c","e","f","z"]

这是改变三元组的地方。 有关如何有效地做到这一点的任何帮助?

3 个答案:

答案 0 :(得分:1)

以下两个选项都假设您的NamePciker()函数可以生成至少5个不同的值,因此存在满足您要求的数组。

通过不开始生成如此多的重复项,可以更好地处理您的需求。如果你想要的只是一个名字数组,当每个名字不能重复两次以上时,试试这个:

var buttonValues = [String]()
var dict = [String: Int]()

while buttonValues.count < 10 {
    let name = NamePicker()
    let count = dict[name] ?? 0

    guard count < 2 else { continue }

    buttonValues.append(name)
    dict[name] = count + 1
}

如果您已有阵列并想要更正它,请执行以下操作:

var buttonValues = ["a","a","a","b","b","c","c","e","f","c"]

// Scan the array to tally how many times each name appears
var totalDict = [String: Int]()
buttonValues.forEach { totalDict[$0] = (totalDict[$0] ?? 0) + 1 }

// Now scan it again to update names that appear too many times
var runningDict = [String: Int]()
for (index, value) in buttonValues.enumerated() {
    let count = runningDict[value] ?? 0

    if count >= 2 {
        while true {
            let newValue = NamePicker()
            let newTotal = (totalDict[newValue] ?? 0) + 1
            if newTotal < 3 {
                buttonValues[index] = newValue
                totalDict[newValue] = newTotal
                break
            }
        }
    } else {
        runningDict[value] = count + 1
    }
}

答案 1 :(得分:0)

字典是我认为最好的方式。键是字符,值是该字符的计数。您的运行时将是O(n),因为您只需要遍历每个输入一次。这是一个例子:

let chars = ["a","a","a","b","b","c","c","e","f","c"]

var dict = [String: Int]()

for char in chars {
    //If already in Dictionary, increase by one
    if var count = dict[char] {
        count += 1
        dict[char] = count
    } else {//else is not in the dictionary already, init with 1
        dict[char] = 1
    }
}

输出:

["b": 2, "e": 1, "a": 3, "f": 1, "c": 3]

现在我不确定你想如何第三次替换同一个字符的值,但这可能是对字符串进行分组以确定超出限制的最佳方法。

答案 2 :(得分:0)

我建议自动创建正确的数组,而不是插入错误的值然后检查值是否正确。

//array for button names
var buttonValues = Array<String>()
//tracks what value has been inserted how many times
var trackerDict = [String: Int]()
for i in 0...9 {

    //we initialize a new variable that tells us if we found a valid value (if the value has not been inserted 2 times already)
    var foundValidValue = false
    while !foundValidValue{
        var val = NamePicker()
        //now we check if the value exists and if it is inserted less than 2 times
        if let count = trackerDict[val] {
            if count < 2 {
                foundValidValue = true
            }
        }
        //if we found the value, we can add it
        if foundValidValue {
            trackerDict[val] = (trackerDict[val] ?? 0) + 1
            buttonValues.append(val)
        }
        //if we did not find it, we just run through the loop again
    }
}

我添加了一个字典,因为跟踪字典中的计数要比每次计算数组中出现的次数更快。