javascript如何解决袜子商人问题

    科技2026-08-05  6

    For this week’s algorithm, I picked a problem named Sock Merchant from HackerRank. I slightly adjusted the challenge as we will not be using a second parameter for the number of socks:

    对于本周的算法,我从HackerRank中选择了一个名为Sock Merchant的问题。 我略微调整了挑战,因为我们不会在袜子数量上使用第二个参数:

    Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.

    给定代表每个袜子颜色的整数数组,请确定有多少对颜色匹配的袜子。

    The problem states that we are given an array of integers, where each number represents a distinct color of socks. We will need to write a function that returns the total number of matching pairs of socks that exists in the list.

    问题指出,我们得到了一个整数数组,其中每个数字代表袜子的不同颜色。 我们将需要编写一个函数,该函数返回列表中存在的匹配的袜子对总数。

    Example:Input: 10 20 20 10 10 30 50 10 20Output: 3

    Explanation of the above example:

    上面的示例说明:

    We will use a simple approach to solve this problem by creating a hash map that keeps track of the representing number of colors as key and the frequency of those colors’ appearance in the array as value. The total number of pairs will be calculated and returned at the end.

    我们将使用一种简单的方法来解决此问题,方法是创建一个哈希图,该哈希图将代表颜色的表示数量作为键,并将这些颜色在数组中出现的频率作为值。 对的总数将被计算并最终返回。

    Let’s dive right into the challenge by listing the steps that will take us to the solution:

    让我们通过列出将我们带到解决方案的步骤来深入研究挑战:

    Create an empty object to store the number of socks of each color as key and their count/frequency as value.

    创建一个空对象,以将每种颜色的袜子的数量存储为键,并将其计数/频率存储为值。 Loop through the given array which contains the colors of each sock and check if the current element exists in the map as key.

    遍历给定的数组,其中包含每个袜子的颜色,并检查当前元素是否作为键存在于地图中。 If it does; increment its value by 1.

    如果有的话; 将其值增加1。 If it doesn’t, add the element to the map as key with a value of 1.

    如果不是,请将元素作为键添加到地图中,其值为1。 After the loop finishes, declare a pair counter variable to add the number of pairs for each key.

    循环完成后,声明一个对计数器变量,以添加每个键的对数。

    Iterate through each key of the map, divide their values by 2, round it down with Math.floor to eliminate unpaired socks by keeping only the integer part of the division, and add the result to the counter.

    遍历映射的每个键,将其值除以2,然后用Math.floor向下舍入,通过仅保留除法的整数部分来消除未配对的袜子,然后将结果添加到计数器。

    Return the total count after the loop finishes.

    循环结束后返回总数。

    Here is the solution in JavaScript:

    这是JavaScript中的解决方案:

    We need to find out how many pairs of socks are present in a given array which contains a list of socks of different colors. We first create an object called socks with each color as a key and the occurrence of each key as a value. With a second for loop, we find the number of sock pairs of each available color and then return the sum (pairs) of those pairs. Here is our solution again without comments:

    我们需要找出给定阵列中有多少双袜子,该阵列包含不同颜色的袜子列表。 我们首先创建一个名为socks的对象,将每种颜色作为键,并将每个键的出现作为值。 在第二个for循环中,我们找到每种可用颜色的袜子对的数量,然后返回这些对的总和( pairs )。 这又是我们的解决方案,没有任何评论:

    复杂 (Complexity)

    The time complexity of this approach is O(n) where n is the size of the given array. This is an efficient solution because it loops through the array only once and then loops through the keys of the socks object. The space complexity of this solution is also O(n) because extra space is required for the map we created.

    这种方法的时间复杂度为O(n) ,其中n是给定数组的大小。 这是一种有效的解决方案,因为它仅循环遍历数组一次,然后循环遍历socks对象的键。 此解决方案的空间复杂度也是O(n),因为我们创建的地图需要额外的空间。

    I hope you enjoyed solving this coding challenge. Thank you for reading and please check out my other articles if you want to read about different algorithms solved in JavaScript:

    希望您喜欢解决此编码挑战。 感谢您的阅读,如果您想阅读JavaScript中解决的不同算法,请阅读我的其他文章:

    普通英语JavaScript (JavaScript In Plain English)

    Did you know that we have three publications and a YouTube channel? Find links to everything at plainenglish.io!

    您知道我们有三个出版物和一个YouTube频道吗? 在plainenglish.io上找到所有内容的链接!

    翻译自: https://medium.com/javascript-in-plain-english/javascript-how-to-solve-the-sock-merchant-problem-58e7d487db11

    Processed: 0.008, SQL: 9