![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
How to find repeated number in a array, need algorithm! Urgent!How find a repeated number in a array .....who can tell me the algorithm!
like 1 2 3 5 6 1 output 1 |
|
#2
|
|||
|
|||
Re: How to find repeated number in a array, need algorithm! Urgent!Pseudocode:
Code:
Take your number. Compare it to every element in the array. If it exists in the array, count it. If at the end of the array, your count is greater than 1, say that the number you found exists in the array more than once. Take that idea, make some C++ code, and post any questions |
|
#3
|
|||
|
|||
Re: How to find repeated number in a array, need algorithm! Urgent!Actually, this is quite easy.
Assume the first number array is repeated number and compare to the element of array. If they equal, return number, else return 0; I hope this may help you understand C++ better. |
|
#4
|
|||
|
|||
Re: How to find repeated number in a array, need algorithm! Urgent!Quote:
1. Why assume it is already repeating before searching through array? That would mean the first match which could be the only match and therefore not repeating, would return true. 2. Why return 0? What if the value you're looking for as a repeat value is 0? |
|
#5
|
|||
|
|||
Re: How to find repeated number in a array, need algorithm! Urgent!Quote:
The logic behind this scene is if i not assume the first array to be repeated number, then you need to compare many times but if you assume the first array is repeated number, then you only need to compare the array size - 1; Quote:
This is just an example. What i doing here is if i find the repeated number, then i return true(1), else false(0), you can return the repeated number. I hope this may helps. |
|
#6
|
|||
|
|||
Re: How to find repeated number in a array, need algorithm! Urgent!Quote:
Quote:
|
|
#7
|
|||
|
|||
Re: How to find repeated number in a array, need algorithm! Urgent!I hope this can help other programmers.
|
|
#8
|
|||
|
|||
Re: How to find repeated number in a array, need algorithm! Urgent!Quote:
Just trying to have a clearer picture..... I think the question is to find if the array has a repeated number in it, and not find a particular number in it. Correct me if i am wrong..... I have two methods in mind. Method 1 (easiest) ---------------------- Using nested for loops Code:
Method 2 ---------------------- First sort the array using some sorting algorithm probably quick sort. Maybe u can find in <algorithm> if i am not wrong. Then traverse the sorted array. Comparing the current element with the next element. If equal return some value indicationg repeated found. The difference is that method one uses nested loop while method 2 usesjust one for loop. As the array size grows method 2 will be a better choice if a suitable sorting algorithm is used. To return the repeated number just return the value...... BUT this only work forj ust ONE repeated number. If u want to find the repeated NUMBERS, maybe u can use another array/pointer to store the list of repeated number. Hope this helps!!!! |
|
#9
|
|||
|
|||
I done it in my way .Thank you guys for helping me , I actually done this part by myself ,there is code ,
CPP / C++ / C Code:
Last edited by admin II : 28-May-2007 at 07:32.
Reason: [tagindicator] should be [CPP] ... [/CPP] for C++ code
|
Recent GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Converting a number amount to text | Godzilla | C++ Forum | 5 | 31-Mar-2006 11:38 |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 19:31 |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 21:26 |
| Using an array and finding the element number (subscript) | tommy69 | C Programming Language | 27 | 05-Apr-2004 12:23 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The