Help finding duplicates within an array

Please help don't know how to do it.
I'm done with what I'm supposed to do in my class assignment but the next part
"find the same value within the array" which I just can't figure out.
so this what I have done so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <time.h>
#include <ctime>

using namespace std;

int main()
{
	srand((unsigned int)time(0));
	int array[20];
	int i, highest = 0;

	for ( i = 0; i < 20; i++)
	{
		array[i] = rand() % 100 + 1;
		if (i < 20)
		{
			cout << array[i] << " ";
		}
		if (array[i] > highest)
		{
			highest = array[i];
		}
	}

	cout << "\n\n";
	cout << "The Largest integer in the array is " << highest << endl;



}

Please make simple lines of code for me plz
Last edited on
Hello Growthra,

It is always helpful to post the complete assignment that you were given. This way no one has to guess at what you need to do.

My question is do you need to check for duplicates when the array is assigned a number or is it to check after the for loop is finished?

I revised you code a bit. Note the comments:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
//#include <time.h>  // <--- Use C++ <ctime> not the C header file.
#include <ctime>
#include <cstdlib>   // <--- For "srand" and "rand".

using namespace std;  // <--- Best not to use.

int main()
{
    constexpr int MAXSIZE{ 20 };

    //srand((unsigned int)time(0));
    srand(static_cast<unsigned int>(time(nullptr)));

    int array[MAXSIZE]{};  // <--- ALWAYS initialize your variables.
    int i{}, highest = 0;  // <--- "i" should be defined in the for loop. The only place it is used.

    for (i = 0; i < MAXSIZE; i++)
    {
        array[i] = rand() % 100 + 1;

        //if (i < 20)  // <--- This if statement is not really needed. you can do without it.
        //{
            cout << array[i] << " ";
        //}

        if (array[i] > highest)
        {
            highest = array[i];
        }
    }

    //cout << "\n\n";
    cout << "\n\nThe Largest integer in the array is " << highest << '\n';  // <--- Changed.

    return 0;  // <--- Not required, but makes a good break point.
}


<time.h"> and <ctime> are the same thing. You only need 1 and <ctime> is the better choice.

Your "srand" is better than I have seen. I offer what I put there as an option which I believe is the more up to date way to code it. Also have a look at https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
About a 30 minute video, but worth the time to understand the problems with "rand".

I can tell you how to check for duplicates, but not knowing what you need to do if you find 1 I am at a bit of a loss.

I am kind of stuck here not knowing is you need to check for a duplicate at the time you generate the random number or after.

Since you are using an array I am thinking nested for loops to compare the first element of the array to the 2nd - last.

I will work on that for a bit.

Andy
Hey thanks for the reply Andy
my assignment breaks into 3 parts

A: Create an array that contains 20 elements of random integers ranging from 1 to 100 then print it

B: Print the top 3 largest numbers

C: Check they are any duplicated values and how many of them within the array then print them.

P.S no using of functions.
Last edited on
Hello Growthra,

I would suggest starting with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <ctime>
#include <cstdlib>   // <--- For "srand" and "rand".

using namespace std;  // <--- Best not to use.

int main()
{
    constexpr int MAXSIZE{ 20 };

    srand(static_cast<unsigned int>(time(nullptr)));

    int numArray[MAXSIZE]{};  // <--- ALWAYS initialize your variables.
    int highest{};  // <--- "idxArr" should be defined in the for loop. The only place it is used.

    for (size_t idx = 0; idx < MAXSIZE; idx++)
    {
        numArray[idx] = rand() % 100 + 1;
    }

    std::cout << "\n ";

    for (size_t idx = 0; idx < MAXSIZE; idx++)
    {
        std::cout << numArray[idx] << ' ';
    }

    std::cout << '\n';

    return 0;  // <--- Not required, but makes a good break point.
}

First the name "array" is not a good name to use. There is a "std::array" that could be a problem should you ever include the header file "<array>".

"i", "j" and "k" are common for for loops, but a proper name for the loop iterator really helps.

Lines 21 - 28 is there just to show you what is in the array. You do not need this for the program, but it helps in testing.

To make it easier to check for duplicates I would sort the array. Then it is very easy to get your top 3 numbers and then to check for duplicates.

Your highest numbers will be in the last 3 elements of the array.

When you say P.S no using of functions. does this mean functions that you would write or something like "std::sort" from the "algorithm" header file?

Not a problem. After you populate the array you could follow with your own sort code.

Andy
Yeh Andy
what I mean is no built-in Functions and Library Functions.
like Headers and int voids().
Only got to use the given headers and my assignments aren't on the functions side yet.
Last edited on
Hello Growthra,

No worries. It is not hard to write your own sort.

What I have come up with so far produces this output:

 7 8 28 30 33 38 45 55 63 65 67 69 74 74 74 86 86 88 91 95

 The three largest numbers are: 88, 91, 95

 There are 3 duplicate in the array.


I did cheat a little:
 
int numArray[MAXSIZE]{ 7, 8, 28, 30, 33, 38, 45, 55, 63, 65, 67, 69, 74, 74, 74, 86, 86, 88, 91, 95 };

Along with commenting out the for loop that creates the array and the sort. This allows you to concentrate on other parts of the program after you know the for loop and sort work.

Printing out the 3 largest is a single "cout" to print the last 3 elements of the array. I would suggest this:
1
2
3
4
std::cout << " The three largest numbers are: " 
    << numArray[MAXSIZE - 3] << ", "
    << numArray[MAXSIZE - 2] << ", "
    << numArray[MAXSIZE - 1] << '\n';

Using "MAXSIZE" works best if you should ever decide to change the size of the array.

Andy
Hello Growthra,

I did write a sort part and now have this output:


 6 7 7 20 21 21 23 40 50 61 61 76 78 81 87 90 94 96 97 99

 The three largest numbers are: 96, 97, 99

 There are 3 duplicate in the array.
 And they are 7 21 61



 1 19 31 34 36 38 39 45 47 52 63 64 68 75 77 88 90 92 92 98

 The three largest numbers are: 92, 92, 98

 There is 1 duplicate in the array.
 And it is 92


I just noticed that the 3 highest numbers, in the 2nd run, have a duplicate. You may want to use "90, 92 and 98", but that is something you will need to decide on.

I am curious as to where you are at so far?

Andy
Last edited on
To check for duplicates you could first sort the array, then create a nested for loop and check every element against the next element such as arr[i] == arr[j] where j is 1 greater than i, if this holds true then a duplicate was found.

Hello adam2016,

Already suggested the sort. And I just needed 1 for loop using if (numArray[idx] == numArray[idx + 1]) inside the loop. This worked well even if there are 3 or more numbers in a group.

Andy
There is no need to sort the array as this isn't required in the spec.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <ctime>
#include <cstdlib>

int main()
{
	constexpr size_t MAXSIZE {20};
	constexpr size_t MAXNUM {100};

	std::srand(static_cast<unsigned int>(std::time(nullptr)));

	int numArray[MAXSIZE] {};
	size_t duplics[MAXNUM] {};

	for (size_t idx = 0; idx < MAXSIZE; ++idx)
		numArray[idx] = std::rand() % 100 + 1;

	std::cout << '\n';

	for (size_t idx = 0; idx < MAXSIZE; ++idx) {
		std::cout << numArray[idx] << ' ';
		++duplics[numArray[idx] - 1];
	}

	std::cout << "\n\nThe top 3 largest numbers are: ";

	for (size_t idx = MAXNUM - 1, cnt = 0; cnt < 3 && idx > 0; --idx)
		if (duplics[idx]) {
			std::cout << idx + 1 << ' ';
			++cnt;
		}

	std::cout << "\n\nThe duplicated numbers are:\n";

	for (size_t idx = 0; idx < MAXNUM; ++idx)
		if (duplics[idx] > 1)
			std::cout << idx + 1 << " duplicated " << duplics[idx] << " times\n";
}



99 89 66 43 4 95 11 3 68 31 16 98 12 89 91 79 31 99 3 58

The top 3 largest numbers are: 99 98 95

The duplicated numbers are:
3 duplicated 2 times
31 duplicated 2 times
89 duplicated 2 times
99 duplicated 2 times

Last edited on
Thank you help for the help, I really neeeded it.
Topic archived. No new replies allowed.