Given the following array, write a program to flatten the array to remove the duplicate part of the data, and finally get an ascending and non-repeating array.
var arr = [ [1.2.2], [3.4.5.5], [6.7.8.9[11.12[12.13[14]]]],10];
Copy the code
Details, all from MDN, as follows:
- Use the Set method to remove the weight
- Flat () Array flattening: The Flat () method recurses through the array at a specified depth and returns a new array with all the elements in the traversed subarray.
- Infinity: Global properties
Infinity
It’s a number that means infinity. - Array.from() : Creates a new, shallow-copy Array instance from an array-like or iterable.
- Sort (): Sorts the elements of an array and returns the array.
Implementation:
let arr = [ [1.2.2], [3.4.5.5], [6.7.8.9[11.12[12.13[14]]]],10];
Array.from(new Set(arr.flat(Infinity))).sort((a,b) = >{
return a-b
})
//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Copy the code
Second, introduce the difference between Set, Map, WeakSet and WeakMap?
- A Set allows you to store a unique value of any type, either a primitive value or an object reference;
- WeakSet members are all objects; Members are weak references that can be collected by the garbage collection mechanism and can be used to hold DOM nodes without causing memory leaks.
- A Map is essentially a collection of key-value pairs, like a collection; You can iterate, you can do a lot of things, you can convert to a variety of data formats.
- WeakMap only accepts objects as key names (except null) and does not accept other types of values as key names; The key name is a weak reference, the key value can be arbitrary, the object that the key name points to can be garbage collected, in this case the key name is invalid; The methods are get, set, has, and delete.
Sort array [3, 15, 8, 29, 102, 22] by sort()
According to array.sort () on MDN, the default sort order is to sort elements by converting them to strings and then comparing their UTF-16 encoding order.
Output:
[102.15.22.29.3.8]
Copy the code
Ps: UTF-16 encoding sequence Who will remember…
How to implement bubble sort, how much time complexity, and how to improve it?
- Time complexity: The amount of time an algorithm takes to execute
- Space complexity: The amount of memory required to run a program.
The principle of bubble sort algorithm is as follows:
-
Compare adjacent elements. If the first one is bigger than the second, swap them both.
-
Do the same for each pair of adjacent elements, starting with the first pair and ending with the last pair. At this point, the last element should be the largest number.
-
Repeat this step for all elements except the last one.
-
Keep repeating the above steps for fewer and fewer elements at a time until there are no more pairs of numbers to compare
function sort(arr){
const array = [...arr]
// Add an identifier, if the array is already sorted, you can jump out of the loop !!!!!
let isOk = true
// Outer loop, control the number of trips, each time find a maximum;
Array.length-1; array.length-1; array.length-1;
for(let i = 0; i < array.length - 1; i++){
// The inner loop controls the number of comparisons and determines the size of two numbers
// 5 numbers
// First compare 4 times (5-1-0)
// Second comparison 3 times (5-1-1)
// The third time to compare 2 times (5-1-2)
// The number of comparisons is array.length - 1 - I
for(let j = 0; j < array.length - 1 - i; j++) {
// If the number in front is large, put it in the back.
if (array[j] > array[j + 1]) {
let temp = array[j]
array[j] = array[j + 1]
array[j + 1] = temp
isOk = false}}console.log(The first `${i+1}Time is more `,array)
if(isOk){
break; }}return array
}
console.log(sort([5.4.3.2.1]))
// First comparison [4, 3, 2, 1, 5]
// Second comparison [3, 2, 1, 4, 5]
// Third comparison [2, 1, 3, 4, 5]
// Fourth comparison [1, 2, 3, 4, 5]
//[1, 2, 3, 4, 5]
Copy the code
If an array is empty or not, add it to the array.
if (arr instanceof Array && arr.length > 1) {
// Bubble method
}
Copy the code