有没有一种方法可以使用Ruby从数组中删除特定的重复项?示例数组:
["hello", "removeme", "removeme", "hello", "testing"]
我只想在该数组中删除"removeme"
的重复项。期望的输出是:
["hello", "removeme", "hello", "testing"]
有没有类似这样的方法可以得到期望的输出?["hello", "removeme", "removeme", "hello", "testing"].uniq('removeme')
["hello", "removeme", "hello", "testing", "removeme"]
,那么期望的结果是什么?我想知道保留元素的选择(这里是第一个或第二个实例的“removeme”)是否重要。 - Cary Swoveland["hello", "removeme", "hello", "testing"]
,我想要删除具有特定字符串的重复项,例如上面的例子中,我想要在数组中保留一个removeme
值。我不能使用uniq
,因为它会影响数组中的hello
值。 - oj5thremoveme
值”,但我问你要保留哪一个。说“期望的结果将是["hello", "removeme", "hello", "testing"]
”表明您希望保留第一个实例,但可能您不关心保留哪一个。您需要澄清这一点。在澄清问题时,最好编辑您的问题而不是在评论中详细说明,因为问题应该是独立的--读者不应该被要求阅读所有评论才能理解问题。 - Cary Swoveland