5

i have the following list of UITextField:

let list = [(name1TextField, phone1TextField), (name2TextField, phone2TextField), (name3TextField, phone3TextField), (name4TextField, phone4TextField), (name5TextField, phone5TextField)]

i'm trying to find phones duplicates and print them out

EDIT

e.g. (tuples could be empty)

list = [("john", "555-444-333"), ("james", "555-444-333"), ("",""), ("bob", "333-222-111"), ("nancy", "222-111-444"), ]

output 555-444-333

how can i do?

Mono.WTF
  • 1,587
  • 3
  • 18
  • 28

4 Answers4

3

Given this

var name1TextField: UITextField!
var phone1TextField: UITextField!
var name2TextField: UITextField!
var phone2TextField: UITextField!
var name3TextField: UITextField!
var phone3TextField: UITextField!
var name4TextField: UITextField!
var phone4TextField: UITextField!
var name5TextField: UITextField!
var phone5TextField: UITextField!

And this

let list = [(name1TextField, phone1TextField), (name2TextField, phone2TextField), (name3TextField, phone3TextField), (name4TextField, phone4TextField), (name5TextField, phone5TextField)]

Solution

let repeatedPhones = list
    .flatMap { $0.1?.text }
    .reduce([String:Int]()) { (var dict, phone) -> [String:Int] in
        dict[phone] = (dict[phone] ?? 0) + 1
        return dict
    }
    .filter { $0.1 > 1 && !$0.0.isEmpty }
    .map { $0.0 }
Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
  • great solution but tuples could be empty, if this happens your code will print "", how can I do to print out only if there are elements inside? – Mono.WTF Feb 16 '16 at 09:28
  • @Mono.WTF: done, I changed the `filter` from this `.filter { $0.1 > 1 }` to this `.filter { $0.1 > 1 && !$0.0.isEmpty }`. Let me know if now it does work fine for you. – Luca Angeletti Feb 16 '16 at 09:34
  • please update I got an error on var dict: var parameters are deprecated and will be removed in Swift 3 – Mono.WTF Mar 31 '16 at 12:13
1

Using dictionary to record how many time you see a phone number:

var dict = [String: Int]()

And then go thru the whole list:

for (_, phone) in list {

    if let count = dict[phone] {
        dict[phone] = count + 1
    } else {
        dict[phone] = 1
    }
}

After this you will have a dictionary which contains the phone number and the count of each phone number appear in the list

for item in dict {
    if item.1 > 1 {
        print(item.0)
    }
}

This method has a time complexity: O(2n)

And this question looks like a duplicate of Find Duplicate Elements In Array Using Swift

Community
  • 1
  • 1
Breek
  • 1,431
  • 13
  • 24
0

You can create a list of the last tuple items and then, as you add them to a new array, check if they are already contained in the array. So something like:

func processList(list) -> String {
     var bufferArray[String] = []
     for (int i = 0; i < list.size; i++) {
          if !(bufferArray.contains( list[i].1 )) {
               bufferArray.add(list[i].1)
          else {
             return list[i].1
          }
      }
}
Cole
  • 2,641
  • 1
  • 16
  • 34
0

What I would do is the following:

var duplicates = []
var set = Set<String>()

for tuple in list {
    if set.contains(tuple.phoneTextField.text) {
        duplicates.append(tuple.phoneTextField.text)
    } else {
        set.insert(tuple.phoneTextField.text)
    }
}

At the end you would do whatever you want with the duplicates array.

thyago stall
  • 1,654
  • 3
  • 16
  • 30