counting unique elements of a sentence

I need to use a template function to count the unique elements in an array. I just cannot figure it out. I was able to get the first two output right but not the last two. The problem is this: the program I wrote just compare the two elements that are next to each other. If they are not the same, then it add one counter to unique. I need to know how to compare all of the element each other. Any help would be appreciated.

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
#include <string>

using namespace std;

template<typename T>
int NumUnique(const T *array, int n)
{
  int unique = 1;

  for(int i = 0; i < n - 1; i++)
     if (array[i] != array[i+1])
       unique++;

  return unique;
}

template<typename T>
void PrintArray(const T *array, int count)
{
 const T *ptr;
 const T *const endPtr = array + count;

 for (ptr = array; ptr < endPtr; ptr++)
  cout << *ptr << " ";

 cout << endl;
}

template<typename T>
void PrintAndCountUnique(const T *array, int n, string nameOfArray)
{
 int numUnique;

 cout << "Array " << nameOfArray << " contains:" << endl;
 PrintArray(array, n);
 numUnique = NumUnique(array, n);
 cout << "and has " << numUnique << " unique element" << (numUnique > 1 ? "s." : ".") << endl << endl;
}

int main()
{
 const int aCount = 5, bCount = 7, cCount = 7, dCount = 12;
 int a[aCount] = {5, 5, 5, 5, 5};
 double b[bCount] = {7.7, 6.6, 5.5, 4.4, 3.3, 2.2, 1.1};
 char c[cCount] = {’r’, ’a’, ’c’, ’e’, ’c’, ’a’, ’r’};
 string d[dCount] = {"Cadillac", "Oldsmobile", "Chevrolet", "Toyota", "Lexus", "Dodge", "GMC", "BMW", "BMW", "GMC", "Dodge", "Lexus"};

 PrintAndCountUnique(a, aCount, "a");
 PrintAndCountUnique(b, bCount, "b");
 PrintAndCountUnique(c, cCount, "c");
 PrintAndCountUnique(d, dCount, "d");

return 0;
}


what it outputs:

Array a contains:
5 5 5 5 5
and has 1 unique element.

Array b contains:
7.7 6.6 5.5 4.4 3.3 2.2 1.1
and has 7 unique elements.

Array c contains:
r a c e c a r
and has 7 unique elements.

Array d contains:
Cadillac Oldsmobile Chevrolet Toyota Lexus Dodge GMC BMW BMW GMC Dodge Lexus
and has 12 unique elements.


what it should have:

Array a contains:
5 5 5 5 5
and has 1 unique element.

Array b contains:
7.7 6.6 5.5 4.4 3.3 2.2 1.1
and has 7 unique elements.

Array c contains:
r a c e c a r
and has 4 unique elements.

Array d contains:
Cadillac Oldsmobile Chevrolet Toyota Lexus Dodge GMC BMW BMW GMC Dodge Lexus
and has 8 unique elements.
Last edited on
I need to know how to compare all of the element each other.

Either use a nested loop or work on a sorted copy of the array.
Last edited on
1) make a copy of the array
2)compare each element of the original array with element 1 of the copy
3)count the duplicates....if count >1 there is a duplicate... zero counter
4)compare each element of the original array with element 2 of the copy
5)count the duplicates....if count >1 there is a duplicate...zero counter
6) continue until original array iterated through
Topic archived. No new replies allowed.