-1

I can't figure out how to have apart of an array match a whole array and i want to pretend that we don't know that usersInData[0,1,2,3] is the same as verifiedUsers[0,1,2,3], i want to somehow match the same value without using the index of each array.

I tried this-

var usersInData = ["hello", "hello1", "hello2", "hello3", "hello4", "hello5", "hello6", "hello7"]

var verifiedUsers = ["hello", "hello1", "hello2", "hello3"]

if usersInData == verifiedUsers {
    print("special user")
} else {
    print("regular user") 
}

but it prints "regular user", so basically it didn't work. I want the verified users to be different than the regular users. So for example user "hello" gets a special verified icon etc. Thank you!

2

5 Answers 5

2

Is something like this what you're after?

var usersInData = ["hello", "hello1", "hello2", "hello3", "hello4", "hello5", "hello6", "hello7"]

var verifiedUsers = ["hello", "hello1", "hello2", "hello3"]

for user in usersInData {
    if verifiedUsers.contains(where: { $0 == user } )
        print("\(user) is a special user")
    } else {
        print("\(user) is a regular user")
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I'm not quite sure what your looking for here.

var usersInData = ["hello", "hello1", "hello2", "hello3", "hello4", "hello5", "hello6", "hello7"]
var verifiedUsers = ["hello", "hello1", "hello2", "hello3"]

If you want to know if verifiedUsers and usersInData have any elements in common.

if !Set(verifiedUsers).isDisjoint(with: usersInData) {
    print("special user")
} else {
    print("regular user") 
}

If you want to know if every element in verifiedUsers is in usersInData

if Set(verifiedUsers).isSubset(of: usersInData) {
    print("special user")
} else {
    print("regular user") 
}

or

if Set(usersInData).isSuperset(of: verifiedUsers) {
    print("special user")
} else {
    print("regular user") 
}

If you want to know if usersInData contains the subsequence verifiedUsers, then that's a bit trickery.

for i in usersInData.startIndex ..< usersInData.endIndex - verifiedUsers.count {
    if verifiedUsers.elementsEqual(usersInData[i ..< i + verifiedUsers.count]) {
        print("special user")
        break
    }
}

I'm sure there is a better answer for the subsequence problem.

UPDATE

Inspired by @iLandes API, I added wrapped my subsequence test.

extension Array where Element: Equatable {
    func hasSubsequence(_ other: Array) -> Bool {
        for i in startIndex ..< endIndex - other.count {
            if other.elementsEqual(self[i ..< i + other.count]) {
                return true
            }
        }

        return false
    }
}

if usersInData.hasSubsequence(verifiedUsers) {
    print("special user")
} else {
    print("regular user") 
}

Comments

0

You have to use the contains : func contains(_ element: String) -> Bool. Unfortunately the default implementation of this function compare only one element.

You have to extend Array and create func containsArray(array: [Element]) -> Bool.

Let's do it :

extension Array where Element: Equatable {
    func containsArray(array: [Element]) -> Bool {
        return !array.contains { !self.contains($0) }
    }
}

How to use it in your code :

var usersInData = ["hello", "hello1", "hello2", "hello3", "hello4", "hello5", "hello6", "hello7"]
var verifiedUsers = ["hello", "hello1", "hello2", "hello3"]


if usersInData.containsArray(array: verifiedUsers) {
    print("Special user")
} else {

    print("Regular user")
}

Comments

0

I understand that you want to know which value is present in both arrays.

We can use Sets to find them, as known from Set Theory in mathematics.

What you are looking for is called Intersection — an element is found in both set of elements. Mathematical notation: A ∩ B.

let usersInData = ["hello", "hello1", "hello2", "hello3", "hello4", "hello5", "hello6", "hello7"]
let verifiedUsers = ["hello", "hello1", "hello2", "hello3", "hello1212"]
let verifiedUsersInData = Array(Set(usersInData).intersection(verifiedUsers))
print(verifiedUsersInData)

output:

["hello3", "hello", "hello2", "hello1"]

As you can se the result array has another order than the two input array. This is due to the fact that sets don't have an ordering concept.

if maintaining an order is important for you, you have several options:

  • sort the result array by comparing the order to one of the input arrays,
  • wrap the values in the input arrays in a type that has a weight value, order the result array by it, than unwrap,
  • use NSOrderedSet, which keeps the order but isn't working for swift types,
  • find a ordered set implementation online. I don't have any recommendation for any, but you should pick one that promises to have the same BigO characteristics we are used from sets.

Comments

0
var usersInData = ["hello", "hello1", "hello2", "hello3", "hello4", "hello5", "hello6", "hello7"]
    var verifiedUsers = ["hello", "hello1", "hello2", "hello3"]

    for user in usersInData {
        if verifiedUsers.contains(user){
        print("\(user) is a special user")
    } else {
        print("\(user) is a regular user")
    }
    }

try this. it ll work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.