Comparing two char within the same array

Hey guys, I am trying to make a comparison within an array that when you enter a character that is already in the array, it gives you "element is a duplicate".

void Duplicate(char array[], int size){
int i;
char letter;

cout << "Enter 20 letters" << endl;

for (i =0; i < size; i++){
cin >> array[i];

if (values[i] == letter ){
cout << "duplicate element" << values[i] << endl;
}

}
Thanks guys.
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
#include <iostream>

constexpr int CHARS_NUM = 21;

void Duplicate(char array[], int size); // std::array already exists in std namespace

int main()
{
    char letters[CHARS_NUM] {};
    Duplicate(letters, CHARS_NUM);
    std::cout << "letters: " << letters << '\n';
    return 0;
}

void Duplicate(char array[], int size)
{
    std::cout << "Enter " << size - 1 << " letters: ";

    int length {};
    for(int i = 0; i < size; i++) {
        std::cin >> array[i];
        length++;
        for(int j{}; j<length-1; ++j) {
            if (array[j] == array[i]) {
                std::cout << "duplicate element: " << array[i] << '\n';
            }
        }
    }
}

You can get the input into a temporary variable, check if the value is not in the array and only if it isn't add it to the array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Duplicate(char array[], int size)
{
  int i;
  char letter;

  cout << "Enter 20 letters" << endl;

  for (i =0; i < size; i++)
  {
    cin >> letter;
    //cin >> array[i];

    if (values[i] == letter )
    {
      cout << "duplicate element" << values[i] << endl;
    }
    else
    {
      array[i] = letter;      
    }
  }
}


This will work only if the duplicate is at the same position. If you need to avoid duplicates at all you need to write a function to check if the letter is in the array values.
Its not working guys, can anyone have a look at my code and tell me if something is wrong.


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
#include <iostream>
using namespace std;

void testFunction(char [], int);
nt main(){

	const char MAXALPHA = 19;
	char alphas[MAXALPHA];
	int i;

	testFunction(alphas, MAXALPHA);
	
	cout << "The original array is " << endl;
		for (i=0; i < MAXALPHA; i++)
		{
			cout << alphas[i];
		}
	cout << endl;

}
void testFunction(char values[], int size){

	int i;
	char letter;

	cout << "Enter 20 alphabets " << endl;

	for (i=0; i < size; i++){
		cin >> letter;

		if (values[i] == letter)
		{
		cout << "Duplicate element " << values[i] << endl;
		}
		else 
		{
			values[i] = letter;
		}	
	}	
}


Thanks guys
oh, I see.
Hang on. There are multiple problems here.

Last edited on
The reply from Enoizat gives a good idea. You either need to use a nested loop, or use a separate function to search the array (which will have its own loop).

The outer loop is for adding each new letter to the array. The inner loop (or the search function) will search the part of the array which has been filled so far, to look for a matching character.

I would make it a c-string for now and avoid the loop to print it.
Also, you ask for 20 but its 19 long, so those correcitons look like:

const char MAXALPHA = 21; //20, as asked for, and +1 to c-string it, because you need a zero value.

char alphas[MAXALPHA]; //be sure to loop from 0 to < maxalpha. NOT <= .
alphas[20] = 0; //c-string end of string marker.

then you can remove that loop and say:
cout << "The original array is " << alphas << endl;

and then down in the testfunction:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void testFunction(char values[], int size){

	int i;
	char letter;
    bool repeats[256] = {false};
	cout << "Enter 20 alphabets " << endl;

	for (i=0; i < size; i++){
		cin >> letter;
           
		if (repeats[letter])
		{
		cout << "Duplicate element " << values[i] << endl;
		}
		else 
		{
			values[i] = letter; 
			repeats[letter] = true;
		}	
	}	
}


what this does is every time you get a letter, it marks it as gotten from a pool of all the possible (8-bit chars). If you get it again, it finds it as used in the pool and complains.
Last edited on
thank you guys, helped me heaps !!
Topic archived. No new replies allowed.