在列表/数组中查找元素重复的次数

罗宾

我有一个列表/数组,每个条目的形式为[string-> String []]

现在我想找到一个元素随后重复的最大次数

例子:

hash1 -> [method1, method2 , method3]
hash2 -> [method1, method4]
hash3 -> [method1, method5]
hash4 -> [method1, method2, method5]
hash5 -> [method2]
hash6 -> [method2]

答案是4(方法1随后重复了四次。我们无法将方法2视为连续重复的最大次数是3)。

如果能在Java中获得此代码,我将不胜感激。但是其他语言也可以使用。

巴特洛米·莱万多夫斯基(Bartlomiej Lewandowski)

我将使用HashMap保留当前计数,并使用int表示最长的链。

For each entry in list
    For each string in entry
        If string in HashMap, increment the value
        Else add to HashMap with value 1
    Remove all pairs from HashMap that weren't in the last entry
    Update longest chain length

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章