0

Ok lets say I have a list which generates values randomly and sometimes it generates the same value twice.

For example generated int value:

1, 1, 2, 3, 4

Then I have a method called duplicateTracker() and its job is to find duplicates within the list.

I have an idea that it should be done using if else statements. So if it detects duplicate numbers its then true, else false.

How do I do this?

Matt
  • 61
  • 2
  • 12
  • 1
    You can use `if` statement to find if a *small, fixed* number of items contains duplicates. When the number of items is not fixed at compile time, or when it is large (three or four being a practical maximum) you need to use a loop and a container of what you've seen so far, or a pair of nested loops. – Sergey Kalinichenko Dec 06 '16 at 15:36
  • 2
    `if Set(numbers).count < numbers.count { print("duplicates exist") }` – dfrib Dec 06 '16 at 15:37
  • 1
    The easiest way would probably be to add the elements to a set one by one, and check if it's already in the set. If it is, it's a duplicate, else it's new. – Carcigenicate Dec 06 '16 at 15:37
  • http://stackoverflow.com/questions/27541145/how-to-generate-a-random-number-in-swift-without-repeating-the-previous-random-n func randomNumber() -> UInt32 { var randomNumber = arc4random_uniform(10) while previousNumber == randomNumber { randomNumber = arc4random_uniform(10) } previousNumber = randomNumber return randomNumber } – Sahil Dec 06 '16 at 15:38
  • 1
    Do you need to know which numbers have duplicates or only if there are duplicates? – JPetric Dec 06 '16 at 15:41
  • @JPetric yes. THe duplicateTracker method should only detect dupliates of numbers (for example) 1, 10 and 20. It should ignore duplicates of other numbers – Matt Dec 06 '16 at 15:50

2 Answers2

1

This makes use of Foundation methods but given your use case, you might want to consider making us of an NSCountedSet to keep track of your generated numbers. E.g.

let numbersGenerator = AnyIterator { return 1 + arc4random_uniform(10) }
var numbersBag = NSCountedSet()

for num in (0...15).flatMap({ _ in numbersGenerator.next()}) {
    print(num, terminator: " ")
    numbersBag.add(num)
} /* 1 3 2 2 10 1 10 7 10 6 8 3 8 10 7 4 */
print()

numbersBag.forEach { print($0, numbersBag.count(for: $0)) }
/* 1 2
   2 2
   3 2
   4 1
   6 1
   7 2
   8 2
   10 4 */

Since NSCountedSet conforms to Sequence, you can easily extract any "duplicate diagnostics" that you wish using e.g. filter:

print("Numbers with duplicates: ", numbersBag.filter { numbersBag.count(for: $0) > 1 })
// Numbers with duplicates: [1, 2, 3, 7, 8, 10]
dfrib
  • 70,367
  • 12
  • 127
  • 192
0

Given this function:

func checkForDups(_ arr1:[Int], _ arr2:[Int]) -> Bool {
    let arrChecked = Set(arr1).subtracting(Set(arr2))
    if Set(arr1).count != arrChecked.count {
        return true
    }
    return false
}

Here's code that works:

let arr1:[Int] = [1,1,2,3,4,5]
let arr2:[Int] = [1,10,20]
let arr3:[Int] = [10,20,30]
print(checkForDups(arr1, arr2))  // prints true
print(checkForDups(arr1, arr3))  // prints false