how to remove duplicates of an array by using js reduce function

???

arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]];

arr.flat(Infinity).reduce((acc, item) => {
console.log(`acc`, acc, acc.includes)
return !acc.includes(item) ? acc.push(item) : acc;
// TypeError: acc.includes is not a function
}, []);

"use strict"; /**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-11-24
* @modified
*
* @description
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @time O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/ const log = console.log; function func1() {
return 1;
} const test1 = func1();
log(`test1 =`, test1);
// test1 = 1 function func2() {
let arr = [];
let item = 2;
log(`arr.includes(item)`, arr.includes(item), !arr.includes(item));
// arr.includes(item) false true
log(`return`, (!arr.includes(item) ? arr.push(item) : arr));
// return 1
// ??? 三元表达式没有,返回值?
return (!arr.includes(item) ? arr.push(item) : arr);
// return !arr.includes(item) ? arr.push(item) : arr;
} const test2 = func2();
log(`test2 =`, test2);
// test2 = 1 function func() {
let arr = [];
let item = 2;
!arr.includes(item) ? arr.push(item) : arr;
return arr;
} const test = func();
log(`test =`, test);
// [2] /* // error
const test1 = isValid(`((}}`);
// ok
const test2 = isValid(`()[]{}`); log(`test =`, test1 ? `` : ``);
log(`test ok =`, test2 ? `` : ``); */

arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]]; arr.flat(Infinity).reduce((acc, item) => {
console.log(`acc`, acc, acc.includes)
if(!acc.includes(item)) {
acc.push(item);
}
return acc;
}, []);

flat & remove duplicates


"use strict"; /**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-11-24
* @modified
*
* @description
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @time O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/ const log = console.log; // flat & remove duplication
const arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]]; // const test = arr.flat(Infinity).reduce((acc, item) => !acc.includes(item) ? acc.push(item) : acc, []);
const test = arr.flat(Infinity).reduce((acc, item) => {
log(`acc`, acc, acc.includes)
acc = !acc.includes(item) ? acc.push(item) : acc;
// TypeError: acc.includes is not a function
// if(!acc.includes(item)) {
// acc.push(item);
// }
return acc;
}, []); log(`test = `, test); const arr2 = ["a", ["b", "c"], ["d", "e", ["f", "g"]]]; // const test2 = arr.flat(Infinity).reduce((acc, item) => acc.indexOf(item) < 0 ? acc.push(item) : acc, []);
const test2 = arr2.flat(Infinity).reduce((acc, item) => {
log(`acc`, acc, acc.indexOf)
// acc = acc.indexOf(item) < 0 ? acc.push(item) : acc;
// TypeError: acc.indexOf is not a function
if(acc.indexOf(item) < 0) {
acc.push(item);
}
return acc;
}, []); log(`test2 = `, test2); /* // error
const test1 = isValid(`((}}`);
// ok
const test2 = isValid(`()[]{}`); log(`test =`, test1 ? `` : ``);
log(`test ok =`, test2 ? `` : ``); */

[...acc, item] 返回新数组

arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]];

arr.flat(Infinity).reduce((acc, item) => {
console.log(`acc`, acc, acc.includes)
return acc.includes(item) ? acc : acc.push(item);
}, []);

arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]]; arr.flat(Infinity).reduce((acc, item) => {
console.log(`acc`, acc, acc.includes)
return acc.includes(item) ? acc : [...acc, item];
}, []);

问题分析

array.push(item) 返回新数组的新长度

[...array, item] 返回一个新数组

arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]];

arr.flat(Infinity).reduce((acc, item) => {
console.log(`acc`, acc, acc.includes)
// [...array, item] 返回一个新数组
return acc.includes(item) ? acc : [...acc, item];
}, []);
arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]];

arr.flat(Infinity).reduce((acc, item) => {
console.log(`acc`, acc, acc.includes)
acc.includes(item) ? acc : acc.push(item);
// OK,返回一个新数组
return acc;
}, []);
arr = ["a", ["b", "c"], ["d", "e", ["f", "g"]]];

arr.flat(Infinity).reduce((acc, item) => {
console.log(`acc`, acc, acc.includes)
// Error ??? array.push(item) 返回新数组的新长度
return acc.includes(item) ? acc : acc.push(item);
}, []);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

refs

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


how to remove duplicates of an array by using js reduce function的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  2. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  3. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  4. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  5. 26. Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  6. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  7. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  8. 【26】Remove Duplicates from Sorted Array

    [26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...

  9. leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

随机推荐

  1. JVM(四)打破双亲委派和SPI机制

    前言: 我们都知道判断两个类是不是同一个,要根据类加载器和全限定名.这是为什么呢?为什么不同的类加载器加载同一个类是不同的呢? 答案就是,不同的类加载器所加载的类在方法区的存储空间是不同的即Insta ...

  2. Python内存浅析

    Python内存分析 内存机制 转述:内存中的堆栈和数据结构堆栈不是一个概念,可以说内存中的堆栈是真实存在的物理区,数据结构中的堆栈是抽象的数据存储结构. Python的内存机制和Java差不多,分为 ...

  3. net.core.somaxconn net.ipv4.tcp_max_syn_backlog

    Linux参数-net.core.somaxconn与net.ipv4.tcp_max_syn_backlog_梁海江的博客-CSDN博客_net.ipv4.tcp_max_syn_backlog h ...

  4. Linux进程内存用量分析之堆内存篇

    https://mp.weixin.qq.com/s/a6mLMDinYQGUSaOsGYCEaA 独家|Linux进程内存用量分析之堆内存篇 姬晨烜 58技术 2019-12-06 导语 本文将介绍 ...

  5. Apache Http Server 一些资料

    配置虚拟主机的例子: http://httpd.apache.org/docs/current/vhosts/examples.html

  6. LOJ10138

    ZJOI 2008 树上的统计 一树上有 n 个节点,编号分别为 1 到 n,每个节点都有一个权值 w.我们将以下面的形式来要求你对这棵树完成一些操作: CHANGE u t :把节点 u 权值改为t ...

  7. ubuntu虚拟机重启后进入initramfs的解决办法

    问题:windows下使用VMware或者自己安装的ubuntu系统出现,不能正常进入系统,而是进入一个以initramfs开头的命令行界面! 原因:不正常的关闭系统,导致系统文件损坏,/dev/sd ...

  8. 手机用itunes安装更新系统

    1.[Shift+更新]:仅对固件进行更新,保留现有资料和已经安装的程序.但是已经越狱的iPhone或iPad禁止使用此方法!否则有后遗症!没有越狱的iPhone或iPad则可以直接使用此方式.2.[ ...

  9. TCP/IP__TCP协议IP协议&IP地址

    IP协议(Internet Protocol) 1.   定位:计算机网络间相互连接进行通信而定义的协议.在因特网中,它是能使连接到网上的所有计算机网络实现相互通信的一套规则,规定了计算机在因特网上进 ...

  10. pandas中的遍历方式速度对比

    对一个20667行的xlsx文件进行遍历测试 import pandas as pd # 定义一个计算执行时间的函数作装饰器,传入参数为装饰的函数或方法 def print_execute_time( ...