Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to remove the repeated numbers in the array in C language

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In order to solve the problem of how to remove the repeated numbers in the array in C language, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Topic description

All the numbers in an array nums of length n are within the range of 0~n-1. Some numbers in the array are repeated, but I don't know how many numbers have been repeated, or how many times each number has been repeated. Please find any duplicate number in the array.

Example:

Enter:

[2, 3, 1, 0, 2, 5, 3]

Output: 2 or 3

Train of thought analysis

The first thing that comes to mind is the brute force method, which is implemented by two for loops, and the disadvantage is obvious: it takes too much time. To go a step further, you can sort the array first and then a for loop, which is easy to find out all the repeating elements and the number of repeats, which is still a long time.

We consider that if every number appears once, then this time is the most perfect. Each subscript I corresponds to the element numbers [I], that is to say, we put it on the numbers [I] for each element in the array. If we find that there are two elements that want to be placed in the same position, this element must be repeated.

That is, the following process:

If numbers [I] = = I, then we think that the element number [I] is in its own place.

Otherwise, the element numbers [I] should be in the position numbers [I], so exchange numbers [I] and numbers [numbers [I]].

Repeat operation 1 until number [I] = = I, then continue with the element at the next location, or number [I] = = numbers [numbers [I], the element repeats.

Code implementation / / # include / / C language

# include

Using namespace std

/ / 2020.05.22

Int findRepeatNumber (int* nums, int numsSize) {

/ / there is no need to add the judgment that the array is empty, the number of elements is 0, and the elements are out of bounds.

For (int I = 0; I < numsSize; iTunes +) {

/ / if the element is not positioned correctly, swap

While (nums [I]! = I) {

/ / check whether it is equal before exchanging

If (nums [I] = = nums [nums [I]])

Return nums [i]

Int temp = nums [I]

Nums [I] = nums [temp]

Nums [temp] = temp

}

}

Return-1

}

Int main ()

{

Int a [] = {2,3,1,0,2,5,3}

FindRepeatNumber (a par 7)

Printf ("% d", findRepeatNumber (aQuery 7))

Return 0

}

The answer to the question about how to remove the repeated numbers in the array in C language is shared here. I hope the above content can be of some help to you, if you still have a lot of doubts to be solved. You can follow the industry information channel for more related knowledge.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report