Diff Two Arrays

题目

Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.

Here are some helpful links:
Comparison Operators

Array.prototype.slice()

Array.prototype.filter()

Array.prototype.indexOf()

Array.prototype.concat()

given code

function diffArray(arr1, arr2) {
  var newArr = [];
  // Same, same; but different.
  return newArr;
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
  • 遍历arr1
  • 在遍历arr1的时候遍历arr2。比较两个元素是否全等。如果全等,那么两个元素的下标都加一,如果不等,则把
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,349评论 0 33
  • 问题: 比较两个数组,然后返回一个新数组,该数组的元素为两个给定数组中所有独有的数组元素。换言之,返回两个数组的差...
    Ytsssss阅读 10,466评论 2 1
  • 最近开始跟着FreeCodeCamp自学js,因为之前Android开发过程中经常接触前端内容,感觉上面的学习节奏...
    刘小强724阅读 2,415评论 2 1
  • 翻看了一下校讯通上的博客,有些文字以现在的心境再也写不出来,看来,有感而发不是说说而已。而且,相对于手机,我更喜欢...
    蓝苏顾洛阅读 1,118评论 0 0
  • 1 现在是凌晨三点,我站在写字楼的窗前往下看,这个城市依旧灯火辉煌。 这个点楼下除了偶尔几个醉汉经过,路上已经没什...
    泉珲院长阅读 2,435评论 0 0