How to set to zero all duplicate elements in int array using C++

1 Answer

0 votes
#include <iostream>
 
using namespace std;
 
int main() {
    int arr[] = { 1, 1, 2, 3, 4, 4, 4, 5, 6, 6, 7, 7, 7, 7 };
    int len = sizeof(arr) / sizeof(arr[0]);
     
    for (int i = 0; i < len; i++) {
        for (int j = i + 1; j < len; j++) {
            if (arr[i] == arr[j]) {
                arr[j] = 0;
            }
        }
    }
     
    for (int i = 0; i < len; i++) {
        cout << arr[i] << endl; 
    }
     
    return 0;
}
 
 
/*
run:
 
1
0
2
3
4
0
0
5
6
0
7
0
0
0
 
*/

 



answered Feb 18, 2019 by avibootz

Related questions

1 answer 170 views
1 answer 162 views
1 answer 153 views
1 answer 123 views
1 answer 172 views
...