How do I remove duplicate values from an array

Hello, I am writing a Go Fish! game in C++. I am currently working on my "checkSet" function. It identifies a set of cards and removes them from the player's hand. Also, it adds a score to the player, and the game continues until the player or computer reaches a score of 4.

For example, if you have 2, 4, 4, 6, 4, 7, 4, "checkSet" will remove all 4's from the hand and add 1 score point. Your hand will be now 2, 6, 7.

How can I implement this in C++?

Note: This program only uses 2's through 9's (only 32 cards are used).

This program is incomplete. We will call this function in the main, and computersTurn & playersTurn(which I did not write yet).

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
using namespace std;

void shuffleCards (int[], int& position);
void drawCards (int ocean[], int& oceanPos, int hand[], int& handSize, int &drawn );
void stealCards(int, int& [], int&, int& [], int&);
void removeCards(int& [], int, int);

int main()

{
	char yesNo = 'y';
	while (yesNo == 'y') {
		int ocean [32];
		for (int i = 0; i < 32; i++)     // initialize the deck
		{
			ocean [i] = 2 + rand() % 9;
		}
		// Declare variables
		int oceanPos;
		int playerHand [32];
		int computerHand[32];
		int playerSize = 0;
		int computerSize = 0;
		int drawn;

		shuffleCards (ocean, oceanPos);     //shuffles the card
		
		for (int i = 0; i < 5; i++)
		{
			drawCards(ocean, oceanPos, playerHand, playerSize, drawn);
		}
		for (int i = 0; i < 5; i++)
		{
			drawCards(ocean, oceanPos, computerHand, computerSize, drawn);
		}
		
		cout << "Would you like to play again?";
		cin >> yesNo;
	}
	return 0;
}

void shuffleCards (int ocean[], int& position)
{
	int i;
	int j;
	int temp; // temporary storage
	for (i = 0; i < 32; i++) {
		j = 1 + rand() % 32;
		temp = ocean[i]; // store in temp
		ocean [i] = ocean[j]; // j fills the empty i
		ocean [j] = temp; // temp(or i) goes to empty j.
	}
	position = 0;
}


void drawCards ( int ocean[], int& oceanPos, int hand[], int& handSize, int &drawn )
{
	drawn = ocean[oceanPos]; 
	handSize++;   // increase the player's cards count
	hand[handSize] = drawn; // make a room for the new card
	oceanPos++; 
}

int countRank ( int rank, int hand[], int handSize ) 
{
	int i;
	int count = 0;
	for ( i = 0; i < handSize; i++ )
	{
		if ( hand[i] == rank )
		{
			count++;
		}
	}

	return count;
}

void stealCards (int rank, int& from[], int& fromSize, int& to[], int& toSize ) // transfers a card to another player
{
	for (int i = 0; i < fromSize-1; )
	{
		if (from[i] == rank)
		{
			
			toSize++;  // increase the size of an array
			to[toSize] = rank;  // the new card is being added to the hand		
		}
	}

	removeCards (int& from[], int&fromSize, int rank);

}

void removeCards (int& from[], int& fromSize, int rank ) // removes a card from a deck after it is given to the player.

{
	for (int i =0; i < fromSize; i++)
	{

		if (from[i] == rank )
			// shift each value one by one
			for (int j = 0; j < fromSize - 1; j++)
			{
				from[i+j] = from [i+j+1];
			}
	fromSize--;
	}
}

void checkSets (int rank, int hand[], int& handSize, int& score, char pronoun[])

{
	int count = countRank (rank, hand, handSize);
	if (count == 4)
	{
		// remove these values from an array

	}
	score++;

}
use two for loops.

1
2
3
4
5
6
7
for(unsigned int x = 0; x < your_array_size; x++)
{
    for(unsigned int y = (x + 1)/* x + 1, because why would we want to double check?? */; y < your_array_size; y++)
    {
         //I'll leave this to you.
    }
}


so, basically, you go through each one, and check to see if there's another one like it. Simple. The first loop is to peg each one, and the second goes through each one after. This eliminates double checking. If you remove all instances of the dup then it shouldn't be a problem.
What if your first value is one of the duplicated value?

Doesn't y = (x+1) mean that you are actually starting from 1st element? I thought you are supposed to start from 0th element?

For example, you have 4, 2, 4, 4, 4, 5 and you would just skip over the first 4 because of y = (0+1) = 1.

array [1] = array [1+2];

Can you explain a little bit further? I am not sure if I understood you're saying here. Thanks.
Last edited on
it will handle that then.

You must read the code and understand it... x is within the nested for-loop's scope, therefore, we can use x, but since we don't want to eliminate every value (by saying that (arr[0] == arr[0])) so, we start from the next element.

1  [(ar[1] == ar[2]), (ar[1] == ar[3]) ...]
2 [(ar[2] == ar[3]), (ar[2] == ar[4])...]
.
.
.


study for loops.
Last edited on
Topic archived. No new replies allowed.