Get Distinct Members of an Array by property [duplicate] - swift

This question already has answers here:
Removing Duplicates From Array of Custom Objects Swift
(4 answers)
Remove objects with duplicate properties from Swift array
(16 answers)
Closed 5 years ago.
I have an array
Contract object contains:
String:id
String:value
Array is:
contract1 = Contract.new()
contract1.id = 2
contract1.value = "Apple"
contract2 = Contract.new()
contract2.id = 2
contract2.value = "Pen"
contract3 = Contract.new()
contract3.id = 1
contract3.value = "Pineapple"
array = [Contract1, Contract2, Contract3]
I would to find out the list of contracts whose IDs are distinct.
I want to have a solution that doesn't make me change the implementation of my object (overriding the isEqual method etc) since I will be using it for more than one object through out my code.
Desired result:
[contract1, contract3] or [contract2, contract3]
Ideally, an extension with additionally a method to only return the values that are being made distinct:
Desired result: [2, 1]
I tried a couple of approaches from similar questions but either the answers are outdated or doesn't fit my need.

Related

sorting two arrays simultaneously in Perl [duplicate]

This question already has answers here:
How do you sort parallel arrays in Perl?
(4 answers)
How to return a sorted list's index in Perl?
(2 answers)
Closed 5 years ago.
I have two arrays.
1. #store_chromosomes( This is an array of arrays)
2. #fitness_values of the same length.
#store_chromosomes = ( ("attr1", "attr2", "attr3"), ("attr4", "attr5", "attr6"), ("attr_m", "attr_n", "attr_o")); #Just an example
#fitness_values=(-0.3456, -0.6789, 0.5678); #just an example
Elements in #fitness_values corresponding to elements in #store_chromosomes, i.e., -0.3456 is generated for ("attr1", "attr2", "attr3") attribute and so on.
I need to arrange the elements in #fitness_values in descending order, i.e., (-0.6789, -0.3456, 0.5678) at the same time I need to arrange the elements of #store_chromosomes corresponding to the order of #fitness_values, i.e., (("attr4", "attr5", "attr6"), ("attr1", "attr2", "attr3"), ("attr_m", "attr_n", "attr_o") ).
For #fitness_values, I can do it by #fitness_values = sort{4b<=>$a} #fitness_values. But how to do it simultaneously for the array #store_chromosomes.

Swift 3: Remove one character from set [duplicate]

This question already has answers here:
Removing duplicate elements from an array in Swift
(49 answers)
Closed 5 years ago.
Say I have a set [A, B, B, C, D], how would I remove just the first B?
If charToDelete = B and I do this:
SlidingWin.remove(charToDelete)
won't it remove all of the B characters?
In Swift lingo, what you have is an array (not a set). If you want to remove the first "B" from an array, you can do this:
if let index = array.index(of:"B")
{ array.remove(at:index) }
[EDIT] example of a functional approach for anagrams:
let set1 = "cabb"
let set2 = "cbabeijbbacbkiie"
let anagrams = zip(set2.indices,set2.indices.dropFirst(set1.count-1))
.map{set2[$0...$1]}
.filter{$0.sorted() == set1.sorted()}

How to create an array by multiplying another array in a functional way? [duplicate]

This question already has answers here:
Repeating array in Swift
(4 answers)
Closed 5 years ago.
I'd like to create an array that contains elements from another array multiplied by some Int value.
Example:
the following code
let arr = [1,2,3]
let multiplier = 3
print(function(arr, multiplier))
should return
[1,2,3,1,2,3,1,2,3]
I know how to make it using nested for loops, but I'm looking for some nifty functional way. I was thinking about map() function, but it iterates over each element of a given array, which is not my use case I suppose.
Main idea:
Create array of arrays,
flatMap to one-dimensional array.
Example:
let arr = [1, 2, 3]
let multiplayer = 3
print(Array(repeating: arr, count: multiplayer).flatMap({ $0 }))

How do I sort a list of Doubles in Scala? [duplicate]

This question already has answers here:
How do I sort an array in Scala?
(7 answers)
Closed 8 years ago.
How do I sort a simple list of Doubles in Scala?
var dubs = List(1.3,4.5,2.3,3.2)
I think my question may not have accurately reflected my specific problem, since I realize now that dubs.sorted will work just fine for the above. My problem is as follows, I have a string of doubles "2.3 32.4 54.2 1.33" that I'm parsing and adding to a list
var numsAsStrings = l.split("\\s");
var x = List(Double);
var i = 0;
for( i <- 0 until numsAsStrings.length) {
x :+ numsAsStrings(i).toDouble;
}
So, I would think that I could just call x.sorted on the above, but that doesn't work... I've been looking over the sortBy, sorted, and sortWith documentation and various posts, but I thought the solution should be simpler. I think I'm missing something basic, regardless.
Use the sorted method
dubs.sorted // List(1.3, 2.3, 3.2, 4.5)

Scala 2 dimensional Array and Arrays [duplicate]

This question already has answers here:
How to create and use a multi-dimensional array in Scala?
(3 answers)
Closed 8 years ago.
I am trying to write my Java ode from in Scala and I think I need some help.
my problem:
Java:
public static int[][] ScoreMatrix = new int[5][20];
Scala:
var ScoreMatrix: Array[Array[Int]] = new Array[Array[Int]](5, 20)
It's not working, don't know why?
Error "too many arguments for constructor Array(_length:int)Array[Array[Int]]"
For initializing 5*20 2D int array you can use:
var ScoreMatrix: Array[Array[Int]] = Array.ofDim[Int](5, 20)
Your code doesn't work because the Array constructor has only one argument, which is the array length.
Consider also
Array.tabulate(5,20)( (x,y) => 1)
which instantiates a 5 by 20 array with Int: 1 (in general a function of x and y).