Filter out duplicate values in array in C++

前提是你 提交于 2021-01-28 05:20:57

问题


I have a row of ten numbers for example:

5 5 6 7 5 9 4 2 2 7

Now I want a program that finds all duplicates and gives them out in the console like 3 times 5, 2 times 2, 2 times 7. While I did code an algorithm that finds duplicates in a row of numbers I can't give them out in the console as described. My program will output:

3 times 5
2 times 5
2 times 7
2 times 2

How can I solve this problem?

#include <iostream>

using namespace std;

int main()
{
  int arr[10];
  int i,j;
  int z = 1;

    for(i = 0; i < 10; i++) {
        cin >> arr[i];
    }
    for(i = 0; i < 10; i++){
        for(j = i+1; j < 10; j++){
            if(arr[i] == arr[j]){
                z++;
            }
        }
        if(z >= 2){
        cout << z << " times " << arr[i] << endl;
        z = 1;
        }

    }


    return 0;
}

回答1:


You need to check that arr[i] is not already found before, like this for example:

if(z >= 2) {
    int found_before = 0;
    for(j = 0; j < i; ++j)
        if(arr[i] == arr[j])
            found_before = 1;
    if(!found_before)
        cout << z << " times " << arr[i] << endl;
    z = 1;
}

which will print:

3 times 5
2 times 7
2 times 2

That way you don't print 5 again.

With your code it would print that it found 5 three times (for the first 5 in your array), and then when it would move to he second 5 in your array, it would forgot about the first 5 in your array, and report that it found 5 twice (itself and the 5th number of the array).




回答2:


You can use the STL here (C++11):

int arr[10];
std::map<int, int> counters;

for (auto item : arr)
{
    cin >> item;
    ++counters[item];
}

std::for_each(counters.begin(), counters.end(), [](const std::pair<int,int>& item)
{
    if(item.second > 1) std::cout << item.second << " times " << item.first << std::endl;
});



回答3:


Why not use STL?

std::map<int, int> counter;
for (i = 0; i < 10; i++)
        counter[arr[i]] ++;
for (i = 0; i < 10; i++) {
    if (counter.count(arr[i]) > 0){
        std::cout << counter[arr[i]] << " times "<< arr[i] << std::endl;
        counter.erase(arr[i]);
    }
}

std::map is a convenient tool for this job. You can easily count up occurrences of a specific number. After counting, you can print the count of each array element. With counter.erase, it's guaranteed that you won't print the same element for multiple times.




回答4:


Why keeping your algorithm idea, I suggest to create sub method:

std::size_t count(const int* arr, std::size_t start, std::size_t end, int value)
{
    std::size_t res = 0;
    for (std::size_t i = start; i != end; ++i) {
        if (arr[i] == value) {
            ++res;
        }
    }
    return res;
}

then your fixed algorithm would be:

for (std::size_t i = 0; i != 10; ++i) {
    if (count(arr, 0, i, arr[i]) != 0) {
        continue; // Already visited
    }
    auto total = count(arr, i, 10, arr[i]);
    if(total >= 2){
        std::cout << z << " times " << arr[i] << std::endl;
    }
}



回答5:


An easy way is to make another array for it, especially if the numbers are not that big.

Lets say you have initialized your array like so: int nums[10] = { 5, 5, 6, 7, 5, 9, 4, 2, 2, 7 }

int result[max(nums)]; //Fill with zeroes, max(nums) is the highest number in the array
for(int i = 0; i < 10; i++) {
    result[nums[i]]++;
}

for(int i = 0; i < max(nums); i++) {
    if (result[i] > 1) cout << result[i];
}

Mind you this isn't optimized for memory. For larger number contents you might want to consider hashmaps.




回答6:


If you don't need performance but rather compact code, then std::multiset with std::upper_bound is an alternative:

#include<set>
#include<iostream>
#include<algorithm>

int main(int a, char** b)
{
  int array[] = {5, 5, 6, 7, 5, 9, 4, 2, 2, 7};

  std::multiset<int> a(std::begin(array), std::end(array));
  for(auto it = a.begin(); it != a.end(); it = std::upper_bound(a.begin(), a.end(), *it))
  {
    if(a.count(*it) > 1)
        std::cout << *it << " times " << a.count(*it) << std::endl;
  }

  return 0;
}


来源:https://stackoverflow.com/questions/47469728/filter-out-duplicate-values-in-array-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!