In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "golang how to use map to achieve array de-duplication", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "golang how to use map to achieve array de-duplication" bar!
Golang array to reuse map
You can use the key unique attribute of the map data type in go to de-duplicate the array
Remove duplicate elements from the strSlice array so that the elements in the array are unique
Var strMap make (string) strSlice: = [] string {"slice", "int", "string", "int", "boolean", "string"} for _, v strMap = range strSlice {strMap [value] = v} / / strMap is: {"slice": "slice", "int": "int", "string": "string", "boolean": boolean "} / / if you want to convert map to slice You can use the append function var secondStr [] stringfor _, value: = range strMap {secondStr = append (secondStr, value)} / / secondStr: {"slice", "int", "string", "boolean"} golang to delete duplicates in the sorted array.
Given a sorted array, you need to delete the repeating elements in place so that each element appears only once, returning the new length of the removed array.
Do not use extra array space, you must modify the input array in place and do it using O (1) extra space.
Given array nums = [1, 1, 1, 2]
The function should return the new length 2, and the first two elements of the original array nums are modified to 1, 2.
You don't need to consider the elements in the array that exceed the new length.
Given nums = [0re0jing1jing1jing1jing2jing2jinme3jpj4]
The function should return the new length 5, and the first five elements of the original array nums are modified to 0, 1, 2, 3, 4.
You don't need to consider the elements in the array that exceed the new length.
First of all, understand the meaning of the question:
0 should be returned when a given array is empty
And no other array space can be introduced, that is, a new array cannot be used to store the results.
If you don't repeat, ignore the elements in the array that exceed the new length.
Then we can use the idea of fast and slow pointer to solve this problem.
Given two cursors left and right
When the subscript of a given array is the same as the value of left and right, the
If it is not the same, then we have to do an operation, that is, to give the value of the current right subscript to the next subscript of left.
Specific code:
Func removeDuplicates (nums [] int) int {/ / if it is an empty slice, then return 0 if len (nums) = 0 {return 0} / / compare the values of adjacent positions with two tags / / if the same, then assign the value pointed to by right to the next left of left, right: = 0,1 for; right < len (nums) regardless of continuing / / when it is different. Right++ {if nums [left] = = nums [right] {continue} left++ nums [left] = nums [right]} fmt.Println (nums [: left+1]) return left+1} Thank you for reading. This is the content of "how golang uses map to achieve array deduplication". After the study of this article, I believe that you have a deeper understanding of how golang uses map to achieve array de-duplication, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.