开发者

Checking for Duplicates NSArray

开发者 https://www.devze.com 2023-01-06 06:24 出处:网络
Can someone help me with some code to check for duplicates in an NSArray for obective C. I did what I want in vb.net but the translation is hard for me.

Can someone help me with some code to check for duplicates in an NSArray for obective C. I did what I want in vb.net but the translation is hard for me.

'True means no duplicates'
Public Shared Function checkDuplicate(ByVal list As ArrayList) As [Boolean]
    Dim [set] As New HashSet(Of Integer)
    For i As Integer = 0 To list.Count - 1
        Dim val As Boolean = [set].Add(list(i))
        If val = False Then
            Return开发者_如何转开发 val
        End If
    Next
    Return True
End Function


static BOOL CheckDuplicate( NSArray* array )
{
  return [[NSSet setWithArray: array] count] == [array count];
}

P.S. you couldn't do an exact one for one translation of your code, because [NSMutableSet addObject:] doesn't tell you whether anything was added.

P.P.S. I just noticed the "true means no duplicates" comment, so I changed < to == in my code. But it would be better to choose a clearer function name, like HasNoDuplicates.

0

精彩评论

暂无评论...
验证码 换一张
取 消