javascript 整数_在javascript中的数组中查找重复的整数

javascript 整数

This is a continuation of my series of posts on algorithms. You can check out my previous post about finding the first non-repeating character in a string here and another on sorting an array by input value here.

这是我关于算法的系列文章的延续。 你可以看看我以前的帖子关于字符串中找到第一个不重复的字符在这里 ,另一个是关于排序由输入值的阵列在这里

提示 (The Prompt)

Given an array of integers, write a function that determines whether the array contains any duplicates. Your function should return true if any element appears at least twice in the array, and it should return false if every element is distinct.

给定一个整数数组,编写一个确定该数组是否包含重复项的函数。 如果任何元素在数组中至少出现两次,则函数应返回true如果每个元素都不同,则函数应返回false

Example

  • For a = [1, 2, 3, 1], the output should be:

    对于a = [1, 2, 3, 1] ,输出应为:

containsDuplicates(a) = true.

containsDuplicates(a) = true

There are two 1s in the given array.

给定数组中有两个1

  • For a = [3, 1], the output should be:

    对于a = [3, 1] ,输出应为:

containsDuplicates(a) = false.

containsDuplicates(a) = false

The given array contains no duplicates.

给定的数组不包含重复项。

我的解决方案 (My Solutions)

From the prompt, what do we know? First, that we need to iterate through the given array. Second, that we need to check the values of the array to see if there are any duplicate numbers. Third, to return true or false depending on whether there are, in fact, duplicates.

从提示中,我们知道什么? 首先,我们需要遍历给定的数组。 其次,我们需要检查数组的值以查看是否存在重复的数字。 第三,根据是否存在重复项来返回truefalse

The first solution I arrived at is pretty explicit in nature, almost to the degree of being clunky, but it gets the job done.

我得到的第一个解决方案本质上是相当明确的,几乎达到了笨拙的程度,但是它可以完成工作。

  • I began by iterating through the given array using a for loop because I know how many iterations we need to make (the length of the input array a).

    我首先使用for循环遍历给定数组for因为我知道我们需要进行多少次迭代(输入数组a的长度)。

  • Next, I created an if statement that checks the current index place (a.indexOf(a[i])) against the last index place of that same number (a.lastIndexOf(a[i])).

    接下来,我创建了一个if语句,将当前索引位置( a.indexOf(a[i]) )与相同编号的最后一个索引位置( a.lastIndexOf(a[i]) )进行比较。

  • If these numbers are the same, it means that the number only appears once in the array, so it returns false.

    如果这些数字相同,则意味着该数字在数组中仅出现一次,因此返回false

  • If these numbers are different, it means that the number appears at least twice in the array, so it returns true.

    如果这些数字不同,则意味着该数字在数组中至少出现两次,因此返回true

function containsDuplicates(a) {
for (let i = 0; i < a.length; i++) {
if (a.indexOf(a[i]) !== a.lastIndexOf(a[i])) {
return true
}
}
return false
}

After that, I figured there had to be a cleaner, more efficient way to do this check, so I kept digging. I decided try to use a new JS tool I found while working on algorithms called Set, which creates a new object of unique values (usually) from a given array.

在那之后,我发现必须有一种更清洁,更有效的方法来进行此检查,因此我不断进行挖掘。 我决定尝试使用在处理名为Set的算法时发现的新JS工具,该工具会从给定数组创建一个(通常)唯一值的新对象。

function containsDuplicates(a) {
return (new Set(a)).size !== a.length;
}
  • First, I created a new Set out of the input array, which turned it into an object of unique values.

    首先,我从输入数组中创建了一个新的Set ,将其变成具有唯一值的对象。

  • Next, I found the size of the new Set object.

    接下来,我找到了新Set对象的大小。

  • Following this, I got the length of the input array and checked to see if the .size and .length differed.

    之后,我得到了输入数组的长度,并检查了.size.length不同。

  • If the numbers differ, it means that there’s at least one duplicate, so it should return true.

    如果数字不同,则表示至少有一个重复项,因此应返回true

  • If the numbers are the same, it means that there’s no duplicate, so it should return false.

    如果数字相同,则表示没有重复,因此应返回false

Here’s a more explicit example of this:

这是一个更明确的示例:

function containsDuplicates(a) {
return (new Set(a)).size !== a.length;
}console.log(a) => [ 1, 2, 3, 1 ]
console.log(new Set(a)) => Set { 1, 2, 3 }
console.log((new Set(a)).size) => 3
console.log(a.length) => 4

I hope this post helped you in your algorithm journey. Happy coding!

希望这篇文章对您的算法之旅有所帮助。 编码愉快!

翻译自: https://medium.com/@weberzt/finding-duplicate-integers-in-an-array-in-javascript-4fa9f0f3c45

javascript 整数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值