# [Codewars - 7kyu解題] Unique string characters 擷取兩個字串陣列中,互不重複的字母
###### tags: `Codewars`,`7kyu`,`Javascript`,`Array`,`.concat()`,`Set()`,`.filter()`,`.has()`,`.includes()`
> Javascript菜鳥紀錄Codewars解題過程
## Instructions 題目
:link: https://www.codewars.com/kata/5a262cfb8f27f217f700000b
:pushpin: **Instructions:**
接收兩組字串,將兩組字串中互不重複的字母依序組合成新字串回傳。
In this Kata, you will be given two strings a and b and your task will be to return the characters that are not common in the two strings.
:bulb: **Examples:**
solve("xyab","xzca") = "ybzc"
--The first string has 'yb' which is not in the second string.
--The second string has 'zc' which is not in the first string.
## My Solution 我的解法
```javascript=
function solve(a,b){
var arr = []
b.split('').forEach(i => arr[i]=true);
a.split('').forEach(i => {if(!arr[i]){arr.push(i);}})
var arr2 = []
a.split('').forEach(i => arr2[i]=true);
b.split('').forEach(i => {if(!arr2[i]){arr2.push(i);}})
return arr.concat(arr2).join('');
};
```
## Solutions(1) 其他更精簡的寫法
```javascript=
function solve(a,b) {
let setA = new Set(a);
let setB = new Set(b);
return [...(a+b)]
.filter(c => setA.has(c) ^ setB.has(c))
.join("");
};
```
## Solutions(2) 其他更精簡的寫法
```javascript=
function solve(a,b){
return (a+b).split("")
.filter(c => !a.includes(c) || !b.includes(c))
.join("");
};
```
## :memo: 學習筆記
:bulb: **JS兩個陣列比較,刪除重複值的巧妙方法**
https://codertw.com/%E5%89%8D%E7%AB%AF%E9%96%8B%E7%99%BC/265216/
> 聰明的方法!
---
:bulb: **JavaScript 陣列處理方法 [filter(), find(), forEach(), map(), every(), some(), reduce()]**
https://wcc723.github.io/javascript/2017/06/29/es6-native-array/
---
:bulb: **Array.prototype.concat()**
https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
> 用來合併兩個或多個陣列。此方法不會改變現有的陣列,回傳一個包含呼叫者陣列本身的值,作為代替的是回傳一個新陣列。
```javascript=
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
console.log(array3);
// expected output: Array ["a", "b", "c", "d", "e", "f"]
```
---
:bulb: **Set.prototype.has()**
https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Set/has
> 對一個指定值元素在 Set 物件中的存在與否回傳一個布林值。
```javascript=
const set1 = new Set([1, 2, 3, 4, 5]);
console.log(set1.has(1));
// expected output: true
```
---
:bulb: **Array.prototype.includes()**
https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
> 判斷陣列是否包含特定的元素,並以此來回傳 true 或 false。
```javascript=
const array1 = [1, 2, 3];
console.log(array1.includes(2));
// expected output: true
```