FCC_Intermediate Algorithm Scripting_Diff Two Arrays

1.任务及要求

Diff Two Arrays


比较两个数组,然后返回一个新数组,该数组的元素为两个给定数组中所有独有的数组元素。换言之,返回两个数组的差异。

如果你被难住了,记得使用 Read-Search-Ask尝试与他人结伴编程、编写你自己的代码。

这是一些对你有帮助的资源:

测试数据:

diff([1, 2, 3, 5], [1, 2, 3, 4, 5]) 应该返回一个数组。

["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] 应该返回 ["pink wool"]

["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] 应该返回 ["diorite", "pink wool"]

["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"] 应该返回 []

[1, 2, 3, 5], [1, 2, 3, 4, 5] 应该返回 [4]

[1, "calf", 3, "piglet"], [1, "calf", 3, 4] 应该返回 ["piglet", 4]

[], ["snuffleupagus", "cookie monster", "elmo"] 应该返回 ["snuffleupagus", "cookie monster", "elmo"]

[1, "calf", 3, "piglet"], [7, "filly"]应该返回 [1, "calf", 3, "piglet", 7, "filly"]

2.我的解法

function diff(arr1, arr2) {

  // First time;
  var newArr = [];

  newArr = arr1.filter(function(isIn){
    if(arr2.indexOf(isIn)==-1){
      return isIn;
    }
  }).concat(arr2.filter(function(isIn){
    if(arr1.indexOf(isIn)==-1){
      return isIn;
    }
  }));
  // Same, same; but different.
  return newArr;

  //或者-or
/*

  return arr1.filter(function(isIn){

      return arr2.indexOf(isIn)==-1;

  }).concat(arr2.filter(function(isIn){

      return arr1.indexOf(isIn)==-1;

  }));

*/

}

diff([1, 2, 3, 5], [1, 2, 3, 4, 5]);

原文地址:https://www.cnblogs.com/yoursatan/p/12401966.html

时间: 2024-10-14 14:20:35

FCC_Intermediate Algorithm Scripting_Diff Two Arrays的相关文章

FCC_Intermediate Algorithm Scripting_Where art thou

1.任务及要求 Where art thou 写一个 function,它遍历一个对象数组(第一个参数)并返回一个包含相匹配的属性-值对(第二个参数)的所有对象的数组.如果返回的数组中包含 source 对象的属性-值对,那么此对象的每一个属性-值对都必须存在于 collection 的对象中. 例如,如果第一个参数是 [{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last

FCC_Intermediate Algorithm Scripting_Search and Replace

1.任务及要求 Search and Replace 使用给定的参数对句子执行一次查找和替换,然后返回新句子. 第一个参数是将要对其执行查找和替换的句子. 第二个参数是将被替换掉的单词(替换前的单词). 第三个参数用于替换第二个参数(替换后的单词). 注意:替换时保持原单词的大小写.例如,如果你想用单词 "dog" 替换单词 "Book" ,你应该替换成 "Dog". 如果你被难住了,记得使用 Read-Search-Ask尝试与他人结伴编程.编

FCC_Intermediate Algorithm Scripting_Sum All Numbers in a Range

1.任务及要求 Sum All Numbers in a Range 我们会传递给你一个包含两个数字的数组.返回这两个数字和它们之间所有数字的和. 最小的数字并非总在最前面. 如果你被难住了,记得使用 Read-Search-Ask.尝试与他人结伴编程.编写你自己的代码. 这是一些对你有帮助的资源: Math.max() Math.min() Array.reduce() 测试数据:  sumAll([1, 4]) 应该返回一个数字. sumAll([1, 4]) 应该返回 10. sumAll

lodash学习笔记之Array方法

今天周末在家无聊学习一下lodash. lodash目前的中文资料很少.而且api好像还被墙了.下面说一下lodash的arrary相关的方法. 1. chunk   英 [t???k]    顾名思义,是对数组进行分块的方法 n. 大块:矮胖的人或物 用法: _.chunk(array,number)  根据number对array进行均等的分块,如果array不能被number平分,则会留下一个余下的块. _.chunk(['a','b','c','d'],-1); //当 size<=1的

jdk1.8新特性应用之Iterable

我们继续看lambda表达式的应用: public void urlExcuAspect(RpcController controller, Message request, RpcCallback done) { if (Util.isEmpty(request)) { return; } Descriptor descriptor = request.getDescriptorForType(); if (Util.isEmpty(descriptor)) { return; } Field

352. Data Stream as Disjoint Intervals (TreeMap, lambda, heapq)

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be: [1, 1

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

350.求两个数组的交集 Intersection of Two Arrays II

Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Note: Each element in the result should appear as many times as it shows in both arrays. The result can be in any ord

350. Intersection of Two Arrays II(LeetCode)

Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Note: Each element in the result should appear as many times as it shows in both arrays. The result can be in any ord