Compare array to reference array and remove duplicates [closed] - ios

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I can't understand how to rewrite this expression in more "swift" and efficient way:
for result in results {
var isExists = false
for ref in referenceArray {
if result.id == ref.id {
isExists = true
break
}
}
if isExists == false {
filteredResults.append(result)
}
}
I tried this:
filteredResults = results.filter { result in
referenceArray.contains { $0.id != result.id }
}
But it gives me empty array.
Thanks.

Correct me if I'm wrong but it sounds like you want to do something like this:
Given a set of items A and set of items B, create a set of items C with only the items that are in B and not in A.
To paraphrase, I think you're looking for the "new" things in B that don't already exist in A.
If this is what you're trying to do you can use a Set. This is a trivial example with Ints, but hopefully it'll help:
let setA = Set([1,2,3,4,5,6,7,8,9,10])
let setB = Set([2,4,6,8,10,13])
// Only the values that overlap both sets
let evens = setA.intersection(setB) // {6, 10, 2, 4, 8}
// Only the values that do not overlap both sets
let odds = setA.symmetricDifference(setB) // {9, 5, 7, 3, 1, 13}
// All unique elements in both sets
let uniqueToBoth = setA.union(setB) // {13, 10, 2, 4, 9, 5, 6, 7, 3, 1, 8}
// Only elements unique to B
let uniqueToB = setB.subtracting(setA) // {13}

Related

How to reset variable of seed on loop using stride with Swift?

I want to reset the variable (seed) on loop using stride with Swift.
I have this code perfectly working on C#
for (int i = 0; i <= 10; i++)
{
//something
i = 0; //restart this value when necessary
}
And I'm trying this with swift
for var i in stride(from: 0, to: 10, by: 1){
//something
i = 0; //I need to reset this value when necessary, but not working
}
The variable "i" change for a second, but then returns to the original value and the behavior is different from C#.
Thanks.
As Alexander responded, the exact thing you're actually asking to do is villainous. I'm inclined to believe that a labeled do statement isn't going to be your best option, either, but it's the simplest solution without seeing any more code.
The following will print 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
var condition = true
loopReset: do {
for i in 0..<10 {
if condition, i > 5 {
condition = false
continue loopReset
}
print(i)
}
}
May use
var i = 0
while i <= 10 {
i += 1
// reset if necessary
}

Sort an array on three variables [duplicate]

This question already has answers here:
Swift - Sort array of objects with multiple criteria
(8 answers)
Closed 5 years ago.
I have an array with playing cards. I would like to sort these on value, color and playability.
I have 4 colors; 1, 2, 3, 4.
I have 3 playability options; 1, 2, 3.
I have 5 values; 1, 2, 3, 4, 5.
I am able to sort the array on color and value. So if the color is the same, the array is sorted on value.
example color-value pairs:
1-2, 1-4, 1-5, 2-2, 3-1, 3-2, 3-5
Code is,
playerCards.sort {
if $0.colorSorter == $1.colorSorter {
return $0.value < $1.value
}
return $0.colorSorter < $1.colorSorter
}
How do I add the third paramater to sort on playability additionally?
What I would like to see (playability-color-value triplets):
1-1-2, 1-1-4, 1-2-2, 1-3-1, 2-1-5, 2-3-1, 2-3-2, 3-3-5
1: sort on playability
2: sort on color
3: sort on value.
Thanks!
Assuming this is your struct
struct Card {
let color:Int
let value:Int
let playability:Int
}
and this is your array
let cards:[Card] = ...
You can sort cards by
playability
color
value
writing
let sorted = cards.sorted { (left, right) -> Bool in
guard left.playability == right.playability else { return left.playability < right.playability }
guard left.color == right.color else { return left.color < right.color }
return left.value < right.value
}

How to remove one duplicate value in an array?

I have two arrays for which I am comparing [Int]
let filter = strongAgainstArray.filter{weakAgainstArray.contains($0)}
This returns an array of common values in the 2 arrays. I then want to go through and remove those values from each array, which I'm doing like so
for item in filter {
for var i = 0; i < strongAgainstArray.count; i += 1 {
if item == strongAgainstArray[i] {
strongAgainstArray.removeAtIndex(i)
print("Removing a Strong against index \(item)")
}
}
for var i = 0; i < weakAgainstArray.count; i += 1 {
if item == weakAgainstArray[i] {
weakAgainstArray.removeAtIndex(i)
print("Removing a Weak against index \(item)")
}
}
}
This works fine, but let's say one of my arrays contains two entries for 12 as an example. How do I only remove one of them? As it stands, all entries of 12 are being removed entirely.
EDIT
I'm now comparing my two arrays using
let commonValues = Array(Set(strongAgainstArray).intersect(weakAgainstArray))
and then those commonValues from each array with
cleanStrongAgainstArray = Array(Set(strongAgainstArray).subtract(Set(commonValues)).sort())
cleanWeakAgainstArray = Array(Set(weakAgainstArray).subtract(Set(commonValues)).sort())
This is a much better overall solution, but I'm still eventually running into the same issue, albeit slightly different than before.
In the playground, for example...
let array = [7,7,9]
let test = Array(Set(array))
test comes back containing [7, 9], and I need to keep that extra 7. How do I do that?
If the order of the arrays aren't important then you can easily achieve the whole solution using Sets:
let dirtyArray = [1,4,6,1,56,4,4,66,23,3,3,12]
let dirtyArray1 = [3,1,6,99,54]
let cleanArray = Array(Set(dirtyArray).union(Set(dirtyArray1)))
print (cleanArray)
[12, 54, 23, 4, 6, 66, 99, 56, 1, 3]
If order is important, use NSOrderedSet:
let strongAgainstArray = [1, 2, 3, 4]
let weakAgainstArray = [3, 4, 5, 6]
let result = NSOrderedSet(array: (strongAgainstArray + weakAgainstArray)).array

Remove first n elements from array of Int in Swift [duplicate]

This question already has answers here:
How to copy end of the Array in swift?
(6 answers)
Closed 6 years ago.
How can I remove the first n elements from an array of Int in Swift?
For example:
var array = [0, 1, 2, 3, 4, 5, 6]
let n = 4
The result array contains these elements:
[4, 5, 6]
let result = Array(array.dropFirst(n))
(Thanks to KPM and WolfLink for pointing out that let result = array.dropFirst(n) sets result to an ArraySlice which will not remain valid if the original array is released.)
Slightly more succinct than Mr. Johnson's answer:
let result = array.suffix(3)
I'd still go with his because dropFirst is more intuitive / readable than suffix.
You can use a range to slice the Array:
var array = [1,2,3,4,5,6]
let n = 4
print(array[n..<array.count]) //[4,5,6]

Swift Define Array with more than one Integer Range one liner

I have an Array which I have defined
var array: [Int] = Array(1...24)
I then add
array.insert(9999, atIndex: 0)
I would like to do something like
var array: [Int] = Array(9999...9999,1...24)
Is this possible ?
You could simply concatenate the arrays created from each range:
let array = Array(10 ... 14) + Array(1 ... 24)
Alternatively:
let array = [10 ... 14, 1 ... 4].flatMap { $0 }
which has the small advantage of not creating intermediate arrays
(as you can see in the open source implementation https://github.com/apple/swift/blob/master/stdlib/public/core/SequenceAlgorithms.swift.gyb).
As MartinR mentioned, you could simply concenate arrays using the + operator; and if this method is an answer for you, than this thread is a duplicate (see MartinR:s link), and should be closed.
If you explicitly wants to initialize an Int array using several ranges at once (see e.g. hola:s answer regarding array of ranges), you can make use of reduce as follows
let arr = [1...5, 11...15].reduce([]) { $0.0 + Array($0.1) }
Or, alternatively, flatten
var arr = Array([1...5, 11...15].flatten())
Both the above yields the following result
print(arr.dynamicType) // Array<Int>
print(arr) // [1, 2, 3, 4, 5, 11, 12, 13, 14, 15]
For an array of ranges you define the array as
let array: [Range<Int>] = [0...1, 5...100]
and so on and so forth.

Resources