-5

I am having some trouble figuring out how to skip duplicate values in my array below. The first array arrvalid holds the number while arrcpt holds the number of times the number appears. Yet when i run the program it prints both values when i only want the value to be printed once. Is there any way to correct this?

cout<<"Numbers chosen most: ";
int skip =0;

for(x=0;x<countval;x++)
{
    for(q=0;q<countval;q++)
    {
        if((arrvalid[x]==arrvalid[q])&&(arrcpt[x] == 2))
        {
            skip=q;     
        }
    }

    cout<<" "<<arrvalid[skip];

}

Output when q starts at 1: 93 93 34 34 34 34;

C.Browne
  • 1
  • 1
  • 4
    I think it's better if q starts at 1 in the second `for`, because arrvalid[0]==arrvalid[0]. – Bogdan Doicin Feb 02 '19 at 14:19
  • Thanks for the comment i tried your suggest but it printed the numbers twice on each line. I had 93 in two different position in the array and would like it to not print all the times 93 appeared but rather just print that 93 appeared numerous times – C.Browne Feb 02 '19 at 14:30
  • My suggestion doesn't solve the problem. It merely eliminates the number of useless comparisons. – Bogdan Doicin Feb 02 '19 at 14:30
  • I understand that now however is there a way to solve this particular problem. I think there is but I am not sure how to reason it out. – C.Browne Feb 02 '19 at 14:33

1 Answers1

1

Are you looking for the std::set?
Set will make sure you cannot have duplicate records.

Edit: Example code.

cpp.sh

#include <set>
#include <iostream>
int main()
{
    int arr[] = {1,2,3,4,5,1};
    std::set<int> unique(arr, arr+6);
    for (auto i : unique) {
        printf("%i", i);
    }
    return 0;
}
# Output: 12345
nauman
  • 141
  • 1
  • 11
  • 1
    I have never heard about such a function but i will give it a try – C.Browne Feb 02 '19 at 14:34
  • I am not sure, but probably a `std::map` would be more suitable to solve that problem. Also `std::set` is a container class, not a _"function"_. – πάντα ῥεῖ Feb 02 '19 at 14:45
  • Start looking at std::vector. From there you can enhance: https://stackoverflow.com/questions/1041620/whats-the-most-efficient-way-to-erase-duplicates-and-sort-a-vector –  Feb 02 '19 at 14:45
  • There are many solutions to this problem. I prefer set over map when you have simple arrays or when you need to get unique objects from an non-unique source. But again, if it works, it works :) – nauman Feb 02 '19 at 14:53
  • @nauman Apparently the OP also wan'ts to count how often a specific value was _"skipped"_, thus a `std::map` seems to be perfectly apropriate to solve the task. – πάντα ῥεῖ Feb 02 '19 at 16:10
  • i do courage you to provide your own answer if you truly believe its better. im not saying mine is better in any way but instead of pointing out faults, would it not be better if you gave your point of view in a good answer? – nauman Feb 02 '19 at 16:32