-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!

Hamish
  • 78,605
  • 19
  • 187
  • 280
bernan
  • 15
  • 2
  • possible duplicate of https://stackoverflow.com/questions/39161168/how-to-compare-two-array-of-objects and https://stackoverflow.com/questions/36714522/how-do-i-check-in-swift-if-two-arrays-contain-the-same-elements-regardless-of-th – elk_cloner Jun 05 '17 at 13:54
  • 1
    As I can understand from your question, you are trying to find out common values. Right? If yes the create two sets from given two arrays & find the intersection of them. Intersection set will return array. e.g. let fruitsSet = Set(firstArray) let vegSet = Set(secondArray) let output = Array(fruitsSet.intersect(vegSet)) – Gagan_iOS Jun 05 '17 at 13:54

5 Answers5

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")
    }
}
Matusalem Marques
  • 2,399
  • 2
  • 18
  • 28
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") 
}
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
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")
}
Sébastien REMY
  • 2,399
  • 21
  • 39
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.
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
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

karthik
  • 159
  • 9