Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

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

Shulou Source: shulou.com Published: 2022-06-02 07:39:28 11月13日 Update

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.

Tags: Elements numbers arrays locations languages questions two more analysis help loops solutions no obvious easy go further simple subscript number that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Microsoft Docker Shulou Technology NVidia