js數組去重(包含ES6解決方案)

前幾天在codewars作了一道題,這道題的核心問題是數組去重。昨晚以後看到別人的solution,感受本身的solution太low了。javascript

題目

Take 2 strings s1 and s2 including only letters from ato z. Return a new sorted string, the longest possible, containing distinct letters, - each taken only once - coming from s1 or s2.java

有兩個字符串s1和s2,值只能爲a-z。現寫一函數,返回一個新的升序的字符串,其值由s一、s2中的值組成,要求包含最多字符且不能重複。es6

例如:數組

a = "xyaabbbccccdefww"
b = "xxxxyyyyabklmopq"
longest(a, b) -> "abcdefklmopqwxy"

a = "abcdefghijklmnopqrstuvwxyz"
longest(a, a) -> "abcdefghijklmnopqrstuvwxyz"

My Solution

先貼本身的代碼。
個人方案是經過一個新數組存儲字符串,函數getDistinct負責將s一、s2中的字符保存到target數組中且確保不會出現重複字符。
代碼中的Array.fromincludes函數是ES6中的數組方法。瀏覽器

function longest(s1, s2) {
      let distStr,
          value,
          distArr = []
      getDistinct(distArr, s1 + s2)
      // 數組排序並轉成字符串
      distStr = distArr.sort().join('')
      return distStr
    }
    // 數組去重
    function getDistinct(target, source) {
      let value
      // 將字符串轉成數組
      source = Array.from(source)
      for(value of source) {
        // 若是target數組中沒有該value,則將其添加到數組中
        if(!target.includes(value)) {
          target.push(value)
        }
      }
    }

Best Solution

這是全部答案中最精妙的一個,僅用了一行就搞定了。(瞬間發現差距懸殊啊)
這個方案首先利用ES6中提供的Set數據結構對字符串(s1+s2)「去重」,而後結構賦值獲得數組,最後進行排序並轉成字符串。數據結構

const longest = (s1, s2) => [...new Set(s1+s2)].sort().join('')

Other Solution

下面這個方案是我本身方案的ES5版本(不兼容IE8如下的瀏覽器)函數

function longest(s1, s2) {
      var distStr,
          value,
          distArr = []
      getDistinct(distArr, s1 + s2)
      // 數組排序並轉成字符串
      distStr = distArr.sort().join('')
      return distStr
    }
    // 數組去重
    function getDistinct(target, source) {
      var index,
          value
      // 將字符串轉成數組
      source = Array.prototype.slice.call(source, 0)
      for(index in source) {
        value = source[index]
        // 若是target數組中沒有該value,則將其添加到數組中
        if(target.indexOf(value) === -1) {
          target.push(value)
        }
      }
    }

下面這個方案經過新建一個hash對象來記錄已存儲的字符。prototype

function longest(s1, s2) {
      var distStr,
          value,
          distArr = []
      getDistinct(distArr, s1 + s2)
      // 數組排序並轉成字符串
      distStr = distArr.sort().join('')
      return distStr
    }
    // 數組去重
    function getDistinct(target, source) {
      var hash = {},
          index,
          value
      // 將字符串轉成數組
      source = Array.prototype.slice.call(source, 0)
      for(index in source) {
        value = source[index]
        // 若是hash對象中沒有該key,則將其添加到數組中
        if(!hash[value]) {
          target.push(value)
          // 給hash添加一個value屬性,值爲true
          hash[value] = true
        }
      }
    }

還有一種方案是先排序,再比較相鄰的兩個字符,只有當前字符不等於下一個字符的時候,才存儲當前字符。code

function longest(s1, s2) {
      var distStr,
          value,
          distArr = []
      getDistinct(distArr, s1 + s2)
      // 數組排序並轉成字符串
      distStr = distArr.join('')
      return distStr
    }
    // 數組去重
    function getDistinct(target, source) {
      var index,
          value
      // 將字符串轉成數組
      source = Array.prototype.slice.call(source, 0)
      source = source.sort()
      for(index in source) {
        value = source[index]
        // 若是target數組中沒有該value,則將其添加到數組中
        if(value !== source[index + 1]) {
          target.push(value)
        }
      }
    }

結語

因爲這是我人生第一篇博文,用詞和語法有不妥的地方,請多多包含。同時,若是文中有錯誤的地方,請狠狠地吐槽。對象

相關文章
相關標籤/搜索