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 1
s 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.
从提示中,我们知道什么? 首先,我们需要遍历给定的数组。 其次,我们需要检查数组的值以查看是否存在重复的数字。 第三,根据是否存在重复项来返回true
或false
。
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 arraya
).我首先使用
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 整数