Sorting with higher order functions: Giving precedence to one element - swift

With an unordered array of Ints as such:
let numbers = [4, 3, 1, 5, 2]
Is it possible in Swift, using .sorted { }, to order the array with one item prioritised and placed in the first index of the array. So instead of returning [1, 2, 3, 4, 5] we could get [3, 1, 2, 4, 5]?

You can declare a function like this :
func sort(_ array: [Int], prioritizing n: Int) -> [Int] {
var copy = array
let pivot = copy.partition { $0 != n }
copy[pivot...].sort()
return copy
}
Which uses the partition(by:) function.
You could use it like so:
let numbers = [4, 3, 1, 5, 2]
let specialNumber = 3
sort(numbers, prioritizing: specialNumber) //[3, 1, 2, 4, 5]
Here are some test cases :
sort([3, 3, 3], prioritizing: 3) //[3, 3, 3]
sort([9, 4, 1, 5, 2], prioritizing: 3) //[1, 2, 4, 5, 9]
Here an alternative solution that uses sorted(by:) only :
let numbers = [4, 3, 1, 5, 2]
let vipNumber = 3
let result = numbers.sorted {
($0 == vipNumber ? Int.min : $0) < ($1 == vipNumber ? Int.min : $1)
}
print(result) //[3, 1, 2, 4, 5]

Related

Using Swift Get array of non repeating numbers from array of numbers

Example.
let numArray = [1,2,3,3,4,5,5]
From above array create array of non- repeating number using swift. But I don't want to use Set.
Expected output is [1,2,4]
You may try the following:
let numArray = [1, 2, 3, 3, 4, 5, 5]
// Group by value
let grouped = Dictionary(grouping: numArray, by: { $0 })
// Filter by its count, convert back to Array and sort
let unique = Array(grouped.filter { $1.count == 1 }.map(\.key)).sorted()
print(unique) // [1, 2, 4]
Here is an alternative way without using higher order functions:
let numArray = [1, 2, 3, 3, 4, 5, 5]
// Group by value
let grouped = Dictionary(grouping: numArray, by: { $0 })
var uniqueArray = [Int]()
for (key, value) in grouped {
if value.count == 1 {
uniqueArray.append(key)
}
}
print(uniqueArray.sorted()) // [1, 2, 4]

Why isn't there a formSubtracting() method in Swift for Sets

In Swift there is a Form... equivalent for the Sets methods intersection(), symmetricDifference() and union(), i.e. formIntersection(), formSymmetricDifference() and formUnion().
But for the method subtracting() there is no method called formSubtracting. Does anyone know why this is so, because it seams I now have to use something like mySet = mySet.subtracting(anotherSet)
subtract(_:) is what you are looking for:
Removes the elements of the given set from this set.
Example:
var mySet: Set = [1, 2, 3, 4, 5]
let anotherSet : Set = [2, 4, 6, 8]
mySet.subtract(anotherSet)
print(mySet) // [3, 1, 5]
There is also a variant which takes another sequence (of the same element type) as the argument, e.g. an array:
var mySet: Set = [1, 2, 3, 4, 5]
let anotherSequence = [2, 4, 6, 8]
mySet.subtract(anotherSequence)
print(mySet) // [3, 1, 5]

Cannot assign value of type '()' to type '[Int]'

Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.
Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
I have implemented following, but it throws me an error
Line 17: cannot assign value of type '()' to type '[Int]'
By the way, are there more elegant solution?
class Solution {
func sortArrayByParity(_ A: [Int]) -> [Int] {
var oddTemp :[Int] = []
var evenTemp :[Int] = []
for a in A
{
if a%2 == 0
{
evenTemp.append(a)
}
else
{
oddTemp.append(a)
}
}
// error is thrown in the following
return evenTemp += oddTemp
}
}
If you want to mutate the original array:
var a = [3, 1, 2, 4]
a.sort(by: { $0 % 2 < $1 % 2 })
print(a) //prints [2, 4, 3, 1]
If you prefer immutability:
let a = [3, 1, 2, 4]
let result: [Int] = a.sorted(by: { $0 % 2 < $1 % 2})
print(result) //prints [2, 4, 3, 1]
Other solution:
let a = [3,1,2,4]
let result: [Int] = a.reduce(into: []) { accumulator, element in
let newIndex = element % 2 == 0 ? accumulator.startIndex : accumulator.endIndex
accumulator.insert(element, at: newIndex)
}
print(result)
prints [4, 2, 3, 1]
return evenTemp + oddTemp
does what you want
Mutable arrays can be sorted in-place, e.g. for your example you could do
var a = [0, 3, 1, 2, 4, 5, 6, 6, 7, 7, 8,9,10,10,11,11,11,11,12]
a.sort { $1 % 2 > $0 % 2 }
print(a) // [0, 2, 4, 6, 6, 8, 10, 10, 12, 3, 1, 5, 7, 7, 9, 11, 11, 11, 11]
The += operator mutates the left hand side operand and its return value is Void. You need to separate the concanation of the arrays and the return statement into separate lines.
evenTemp += oddTemp
return evenTemp
This
evenTemp += oddTemp
doesn't return anything
evenTemp += oddTemp
return evenTemp

Given two dictionaries containing arrays of different capacities add elements to the lesser populated array

So given this data
var data =
["groupA":
[1, 2, 3, 4, 5, 6],
"groupB":
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
I want this output:
["groupA":
[0, 0, 0, 0, 1, 2, 3, 4, 5, 6],
"groupB":
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
This is the best answer I was able to come up with but I feel like its lacking as I have access to the accumulator that I want to mutate within the reduce function.
let maxElement = data.reduce(data.first!) { (acc, obj) in
return acc.value.count > obj.value.count ? acc : obj
}
for dict in data {
if dict.value.count < maxElement.value.count {
var mutableValues = dict.value
mutableValues.insert(0, at: 0)
data[dict.key] = mutableValues
}
}
I think I'm not understanding how to best refactor my reduce function.
If, like me, you don't like for loops, how about this:
data.reduce(data.values.reduce(0){max($0,$1.count)})
{ data[$1.0] = Array(repeating:0,count:$0-$1.1.count) + $1.1; return $0}
You can get the maximum count of your arrays and create an array of zeros with the difference to append to the lesser populated arrays as follow:
var dict: [String:[Int]] = ["groupA": [1, 2, 3, 4, 5, 6],
"groupB": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
let maxCount = dict.values.map{ $0.count }.max() ?? 0
for (key, value) in dict {
let difference = maxCount - value.count
if difference > 0 {
dict[key] = repeatElement(0, count: difference) + value
}
}
print(dict) // ["groupB": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "groupA": [0, 0, 0, 0, 1, 2, 3, 4, 5, 6]]
As per your question you need to refactor your code for the reduce function, I did it the following way.
Hope this helps,
let maxElement = data.reduce(data.first!) {
$0.value.count > $1.value.count ? $0 : $1
}
further I modified the code you provided to achieve the results you were trying.
and the code worked well for me.
let maxElement = data.reduce(data.first!) {
$0.value.count > $1.value.count ? $0 : $1
}
let minElement = data.reduce(data.first!) {
$0.value.count < $1.value.count ? $0 : $1
}
for dict in data {
if dict.value.count < maxElement.value.count {
var mutableValues = minElement.value
let arrayOfZeros = Array(repeating: 0, count: maxElement.value.count - minElement.value.count)
mutableValues.insert(contentsOf: arrayOfZeros, at: 0)
data[dict.key] = mutableValues
}
}

Optimal Way to Remove Unique Values from Two Arrays

I have two arrays of [PFObjects].
For example (simplified):
arr1: [PFObject] = [1, 2, 3, 4, 5, 6, 7, 8]
arr2: [PFObject] = [1, 2, 3, 4, 5]
What is the optimal way to compare arr1 with arr2 and only keep the duplicates (remove unique values).
So that arr1 looks like:
arr1 = [1, 2, 3, 4, 5]
let array = arr1.filter { arr2.contains($0) }
voilĂ  !
First solution (Looping):
var arr1: [PFObject] = [1, 2, 3, 4, 5, 6, 7, 8]
var arr2: [PFObject] = [1, 2, 3, 4, 5]
var temp: [PFObject] = []
for element in arr1 {
if contains(arr2, element) {
temp.append(element)
}
}
arr1 = temp
You can loop over the first array, check if each element is contained in the array, if it is, you can add it to a temporary array. After looping over every element you can replace the value of the first array with your temporary array.
Second solution (Sets):
var arr1: [PFObject] = [1, 2, 3, 4, 5, 6, 7, 8]
var arr2: [PFObject] = [1, 2, 3, 4, 5]
let set1 = Set(arr1)
let set2 = Set(arr2)
var arr1= Array(set1.intersect(set2)) // [1, 2, 3, 4, 5]
What you do here is:
First you create sets from your arrays
Then you use the intersect method from sets to determine common elements
Finally you transform your set to an array before passing it back to arr1
Of course since you will be using sets, duplicate elements will be lost but I'm guessing that shouldn't be a problem in your case
Third solution (filter):
From the answer of Pham Hoan you can use filters to obtain a subset of arr1, the closure gives you the conditions, here it is that arr2 contains the value you are looking at.
let array = arr1.filter { arr2.contains($0) }
This is obviously the shorter solution in terms of code length.
I do not know which technique would be more efficient if you have very large arrays however.