In Python, this code realizes the objective:
intLst = [1, 2, 3]
f1 = lambda x: x
f2 = lambda x: x * 10
newLst = [f(x) for x in intLst for f in [f1, f2]]
print(newLst) # [1, 10, 2, 20, 3, 30]
but in Dart I was not able to do the same using an anonymous function passed to the map() List method.
You can achieve the same thing using collection for, which allows you to do the same type of things you can do with a list comprehension in python.
void main() {
List<int> intLst = [1, 2, 3];
int Function(int) f1 = (x) => x;
int Function(int) f2 = (x) => x * 10;
List<int> newLst = [
for (var x in intLst)
for (var f in [f1, f2]) f(x),
];
print(newLst); // [1, 10, 2, 20, 3, 30]
}
The alternative would be to use expand rather than map. expand is the same as what some other languages call flatMap.
void main() {
List<int> intLst = [1, 2, 3];
int Function(int) f1 = (x) => x;
int Function(int) f2 = (x) => x * 10;
List<int> newLst = intLst.expand((v) => [f1(v), f2(v)]).toList();
print(newLst); // [1, 10, 2, 20, 3, 30]
}
Here's another way to obtain the same result without using functions:
void main() {
List<int> intList = [1, 2, 3];
List<int> newList = [
for (var x in intList) ...[x, x * 10, x * x],
];
print(newList); // [1, 10, 1, 2, 20, 4, 3, 30, 9]
}
This question already has answers here:
Sort array elements based on their frequency
(8 answers)
Closed 12 months ago.
How to sort an integer array based on a duplicate values count. here less number of duplicates should come first.
input [5, 2, 1, 2, 4, 4, 1, 1, 2, 3, 3, 6]
OutPut [5, 6, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1]
Using Martin's comment, here is another approach which aims to reduce the number of loops and conditions we write ourselves by using some functions provided by swift.
// Input
let numbers = [1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6]
// Count the occurrences based on Martin's comment
let countDict = numbers.reduce(into: [:], { $0[$1, default: 0] += 1 } )
// Get a sorted array of the countDict keys, sorted by value which
// is the number of occurrences
let sortedKeys
= countDict.keys
.sorted { countDict[$0, default: 0] < countDict[$1, default: 0] }
// Initialize an empty array to hold the final sorted numbers
var sortedNumbers: [Int] = []
// Add the elements into the sortedNumbers with in their desired order
for key in sortedKeys {
sortedNumbers.append(contentsOf: repeatElement(key,
count: countDict[key, default: 0]))
}
// prints [5, 6, 4, 4, 3, 3, 1, 1, 1, 2, 2, 2] based on the above input
print(sortedNumbers)
let numbers = [5,2,1,2,4,4,1,1,2,3,3,6]
let sortedNumber = numbers.sorted()
print("Input: ",sortedNumber)
var dict = [Int: Int]()
for item in sortedNumber {
let isExist = dict.contains(where: {$0.key == item})
if !isExist {
dict[item] = 1
} else {
if let value = dict[item] {
dict[item] = value + 1
}
}
}
var finalArray = [Int]()
let sortedArray = dict.sorted { (first, second) -> Bool in
return first.value < second.value
}
for d in sortedArray {
for _ in 1...d.value {
finalArray.append(d.key)
}
}
print("Output: ",finalArray)
Input: [1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6]
Output: [5, 6, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1]
There are two lists:
List<int> list1 = [1,2,3,4,5];
List<int> list2 = [1,2,3,4,5,6,7];
How to find difference between them, i.e. [6,7] ?
List<int> list1 = [1,2,3,4,5];
List<int> list2 = [1,2,3,4,5,6,7];
List<int> filtered = list2.where((i) => !list1.contains(i)).toList();
main() {
List<int> list1 = [1, 2, 3, 4, 5, 9];
List<int> list2 = [1, 2, 3, 4, 5, 6, 7];
var list1WithoutList2 = list1.toList()..removeWhere(list2.contains);
var list2WithoutList1 = list2.toList()..removeWhere(list1.contains);
var diff = [...list1WithoutList2, ...list2WithoutList1];
print(diff); // [9, 6, 7]
}
If you add 2 Sets as input you could have used Set.difference
I need to sort an array of elements based on their frequency, for example:
Input array: [1, 6, 6, 6, 6, 4, 3, 5, 5, 5, 2, 2]
Expected output: [1, 3, 4, 2, 2, 5, 5, 5, 6, 6, 6, 6]
I tried with the code below:
var set: NSCountedSet = [1, 6, 6, 6, 6, 4, 3, 5, 5, 5, 2, 2]
var dictionary = [Int: Int]()
set.forEach { (item) in
dictionary[item as! Int] = set.count(for: item)
}
dictionary.keys.sorted()
print(dictionary)
Description: As 1, 3, 4 occur only once, they are shown at the beginning, 2 occurs two times, 5 three times, 6 four times. And [1, 3, 4] are sorted among them.
Expected result: Time complexity should be O(n)
You can achieve the results in O(nlogn) time by first creating a Dictionary containing the number of occurrences for each element (O(n)), then calling sorted on the Array (Swift uses Introsort, which is O(nlogn)) and using the values from the previously created Dictionary for the sorting. The elements of your array need to be Comparable for sorting to work and Hashable to be able to store them in a Dictionary, which provides O(1) element lookup.
extension Array where Element: Comparable & Hashable {
func sortByNumberOfOccurences() -> [Element] {
let occurencesDict = self.reduce(into: [Element:Int](), { currentResult, element in
currentResult[element, default: 0] += 1
})
return self.sorted(by: { current, next in occurencesDict[current]! < occurencesDict[next]!})
}
}
[1, 6, 6, 6, 6, 4, 3, 5, 5, 5, 2, 2].sortByNumberOfOccurences() // [1, 4, 3, 2, 2, 5, 5, 5, 6, 6, 6, 6]
The above solution preserves the order of elements that occur an equal number of times. If you actually want to sort such elements based on their compared values (which is what your sample output does), you can modify the closure in sorted like below:
return self.sorted(by: {occurencesDict[$0]! <= occurencesDict[$1]! && $0 < $1})
Or even shorter, comparing tuples for sorting:
return self.sorted(by: {(occurencesDict[$0]!,$0) < (occurencesDict[$1]!,$1)})
which produces the sample output you provided, [1, 3, 4, 2, 2, 5, 5, 5, 6, 6, 6, 6]
You can try
let dd = [1, 6, 6, 6, 6, 4, 3, 5, 5, 5, 2, 2]
let res = dd.sorted { f, s in
dd.filter { $0 == f }.count < dd.filter { $0 == s }.count
}
print(res) // [1, 4, 3, 2, 2, 5, 5, 5, 6, 6, 6, 6]
There is no way to sort with O(n) time complexity. Look at the worst case complexity for popular algorithms at Wikipedia.
The better worst-case time complexity is O(nlogn). Here is how we can solve it with O(nlogn) time complexity:
let array = [1, 6, 6, 6, 6, 4, 3, 5, 5, 5, 2, 2]
extension Array where Element: Comparable & Hashable {
func countableSorted() -> [Element] {
var counts = [Element: Int]()
// O(n)
for element in self {
counts[element] = (counts[element] ?? 0) + 1
}
// I think that standart method uses O(nlogn) time complexity.
// O(nlogn) + O(n) approximately equal to O(nlogn).
let sorted = counts.sorted { item1, item2 -> Bool in
if item2.value > item1.value {
return true
}
if item2.value == item1.value {
return item2.key > item1.key
}
return false
}
var result = [Element]()
// O(n)
for item in sorted {
let items = Array(repeating: item.key, count: item.value)
result.append(contentsOf: items)
}
// Total time complexity for worst case scenario is O(nlogn)
return result
}
}
print(array.countableSorted())
// Output: [1, 3, 4, 2, 2, 5, 5, 5, 6, 6, 6, 6]
You can try the below code, this worked properly.
var inputArray = [1, 6, 6, 6, 6, 4, 3, 5, 5, 5, 2, 2]
inputArray.sort()
let freq = inputArray.sorted { f, s in
inputArray.filter { $0 == f}.count < inputArray.filter { $0 == s}.count
}
print(freq)
Not sure about the time complexity.
I want to add a solution in O(n)
Sorting takes O(nLogn) but this question can also be solved without using sorting by help of HashMap in Java because it contains the pairs sorted in accordance to the key.
import java.util.*;
class Simple
{
public static void main(String[] arg)
{ int inputArray[] = {1, 6, 6, 6, 6, 4, 3, 5, 5, 5, 2, 2};
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
Map<Integer,List<Integer>> map2 = new HashMap<Integer,List<Integer>>();
for(int i: inputArray)
{
if(map.get(i) == null){
map.put(i, 1) ;
}
else{
int a = map.get(i);
map.put(i,a+1);
}
}
// using for-each loop for iteration over Map.entrySet()
for (Map.Entry<Integer,Integer> entry : map.entrySet()) {
if(map2.get(entry.getValue()) == null){
map2.put(entry.getValue(), new ArrayList<Integer>()) ;
}
map2.get(entry.getValue()).add(entry.getKey());
}
for(Map.Entry<Integer,List<Integer>> entry : map2.entrySet()){
for(int j=0; j<entry.getValue().size(); j++){
for(int i=0; i<entry.getKey(); i++){
System.out.print(entry.getValue().get(j) + " ");
}
}
}
}
}
In First for loop I am iterating through array saving pair of (value,Occurrence) in map1(HashMap). This will take O(n) as HashMap put operation(insertion) takes O(1).
In second for loop I am iterating map1 and inserting pair of (occurrence, list of numbers in the given array with that occurrence) in map2(HashMap2).
Now in last for loop I am iterating through map2 and printing all the lists one by one it means I am printing each element of given array once i.e. I am iterating through the list of each key and printing each element of the list key number of times. So this would also take O(n).
more about HashMap
Time Complexity : O(n)
Swift Version of above code
extension Array where Element: Comparable & Hashable {
func sortByNumberOfOccurences() -> [Element] {
let occurencesDict = self.reduce(into: [Element:Int](), { currentResult, element in
currentResult[element, default: 0] += 1
})
let dict = occurencesDict.sorted(by: {$0.0 < $1.0})
var dictioanary = [Int:Array]()
for (element,occurence) in dict {
if dictioanary[occurence] == nil
{
dictioanary[occurence] = Array()
}
dictioanary[occurence]?.append(element)
}
var resultArray = Array()
let finalDict = dictioanary.sorted(by: {$0.0 < $1.0})
for (frequency,allValuesOccuringWithThisFrequncy) in finalDict {
for i in allValuesOccuringWithThisFrequncy
{
var j = 0
while(j < frequency)
{
resultArray.append(i)
j = j + 1
}
}
}
print(resultArray)
return resultArray
}
}
Time Complexity in Swift O(nLogn)
var inputArray = [1, 6, 6, 6, 6, 4, 3, 5, 5, 5, 2, 2]
var map:[Int: Int] = [:]
for element in inputArray {
let count = map[element]
if count == nil {
map[element] = 1
} else {
map[element] = count! + 1
}
}
var keysArray = map.keys
let sortedKeys = keysArray.sorted { (number1, number2) -> Bool in
if map[number1]! == map[number2]! {
return number1 < number2
} else {
return map[number1]! < map[number2]!
}
}
var finalArray: [Int] = []
for element in sortedKeys {
for _ in 1...map[element]! {
finalArray.append(element)
}
}
print(finalArray)
Time Complexity: O(nlogn)
Try this solution. It worked for me like a charm :)
func numberOfOccurences(in array: [Int], of element: Int) -> Int {
let object = NSCountedSet(array: array)
return object.count(for: element)
}
var inputArray = [1, 6, 6, 6, 6, 4, 3, 5, 5, 5, 2, 2]
var uniqueElements = Array(Set(inputArray))
var otherArray: [Int] = []
var duplicateElements = uniqueElements.filter { (element) -> Bool in
return (inputArray.contains(element) && numberOfOccurences(in: inputArray, of: element) > 1)
}
uniqueElements = uniqueElements.filter({ !duplicateElements.contains($0) }).sorted()
for item in duplicateElements {
let occurences = numberOfOccurences(in: inputArray, of: item)
for _ in 0...occurences - 1 {
otherArray.append(item)
}
}
otherArray = otherArray.sorted()
duplicateElements.removeAll()
let mergedArray = uniqueElements + otherArray
print(mergedArray)
I think this kind of sorting can be achieved in O(n), with something like the following:
let input = [1, 6, 6, 6, 6, 4, 3, 5, 5, 5, 2, 2]
// build the frequency dictionary (easy task)
let frequencies = input.reduce(into: [:]) { $0[$1] = ($0[$1] ?? 0) + 1 }
// allocate a 2-D array of ints, each item in this array will hold the numbers that
// appear I times in the input array
let frequencyTable: [[Int]] = frequencies.reduce(into: Array(repeating: [Int](), count: input.count)) {
// substracting one as arrays are zero indexed
// we can't get of of bounds here since the maximum frequency is the
// number of elements in the input array
// Also replicating the numbers by their frequency, to have
// the same contents as in the input array
$0[$1.value - 1] += Array(repeating: $1.key, count: $1.value)
}
// lastly, simply flatten the 2-D array to get the output we need
let output = frequencyTable.flatMap { $0 }
print(output)
Sample result:
[4, 1, 3, 2, 2, 5, 5, 5, 6, 6, 6, 6]
Note that the order of numbers with the same frequency might differ based on how the dictionary hash function works.
Also we sacrifice space (allocated 2-D array) in favour of time.
The frequencyTable contents will look similar to this (again the order of 1, 4, 3 might differ):
[[4, 3, 1], [2, 2], [5, 5, 5], [6, 6, 6, 6], [], [], [], [], [], [], [], []]
What is the equivalent of this python dictionary in Dart?
edges = {(1, 'a') : 2,
(2, 'a') : 2,
(2, '1') : 3,
(3, '1') : 3}
You could use package:collection's EqualityMap to define a custom hash algorithim that uses ListEquality. For example, you could do this:
var map = new EqualityMap.from(const ListEquality(), {
[1, 'a']: 2,
[2, 'a']: 2,
});
assert(map[[1, 'a']] == map[[1, 'a']])
This will be a heavier weight implementation of Map, though.
You have differents way to do this
1. Using a List
var edges = <List, num>{
[1, 'a']: 2,
[2, 'a']: 2,
[2, '1']: 3,
[3, '1']: 3
};
Simple to write, but you won't be able to retrieve data with
edges[[2, 'a']]; // null
Except if you use const
var edges = const <List, num>{
const [1, 'a']: 2,
const [2, 'a']: 2,
const [2, '1']: 3,
const [3, '1']: 3
};
edges[const [2, 'a']]; // 2
2. Using Tuple package
https://pub.dartlang.org/packages/tuple
var edges = <Tuple2<num, String>, num>{
new Tuple2(1, 'a'): 2,
new Tuple2(2, 'a'): 2,
new Tuple2(2, '1'): 3,
new Tuple2(3, '1'): 3
}
edges[new Tuple2(2, 'a')]; // 2
var edges = {[1, 'a'] : 2,
[2, 'a'] : 2,
[2, '1'] : 3,
[3, '1'] : 3};
Except that you won't ever be able to look up those keys, because a new instance of [1, 'a'] will be a different object.