简体   繁体   中英

How can I find minimum and unique element of array in c++?

I have array(of numbers) with size N. I need to find minimum element which is unique,so if arr[5] ={1,2,3,1,2} , answer is 3. I tried that with following code:

Int n = sizeof(arr)/sizeof(arr[0]);
sort(arr,arr + n);
for(int i = 0;i<N;i++){
     for(int j = 0;j<N;j++){
        if(arr[i] == arr[j]){
           remove(arr,arr+n,i);
           remove(arr,arr+n,j);
        }
    }
}

But problem is that this only work if I have 2 identical elements of arr.I could create if conditions for number of identical, but I can have 3 or 4 or 1000,so it will be pretty odd. So what is more elagant way to do this? Thank you in advance.

try this code, this uses an unordered map

int m = 2147483647;
    int int_array[] = { 1,2,3,3,1,6,7,7,9 };
    unordered_map<int, int> map;
    for (int i = 0; i < sizeof(int_array) / sizeof(int_array[0]); i++) {
        map[int_array[i]] = map[int_array[i]] + 1;
    }
    unordered_map<int, int>::iterator itr;

    for (itr = map.begin(); itr != map.end(); itr++)
    {
        if (itr->second == 1) {
            if (itr->first < m) {
                m = itr->first;
            }
        }
    }
    printf("minimum unique is %d", m);

One way to do it is to use anunordered_map to store the frequencies of each element. Here, the { key, value } pair stored would be { the value of the element, frequency of the element in the array } . Once you store them, you can iterate once on all the elements stored in the unordered_map to find the least element whose frequency is 1 .

after sorting the array you can count the number of the dublicated members, if the member is unique then its count equals zero:

int main()
{
    int arr[] = { 1, 2, 1, 3, 4, 1, 2 };
    int n = sizeof(arr) / sizeof(int);
    sort(arr, arr + n);
    int count = 0;
    int unique = -1;
    for (int i = 0; unique == -1 && i < n - 1; ++i) {
        if (arr[i] != arr[i + 1]) {
            if (count==0)
                unique = arr[i];
            else
                count = 0;
        }
        else {
            count++;
        }
    }
    if (count == 0 && unique ==-1)
        unique = arr[n-1];
    cout << unique;
    return 0;
}

Using what I said in the comments, with an extra bool.

int arr[] = { 1, 2, 1, 3, 4, 1, 2 };
int n = sizeof( arr ) / sizeof( int );

std::sort( arr, arr + n );

bool match = false;
for ( int i = 0; i < n; ++i ) {
    if ( i == n - 1 || arr[i] != arr[i + 1] ) {
        if ( match )
            match = false;
        else
            return arr[i];
    } else {
        match = true;
    }
}

return -1; // made up number in case all duplicates

If the two values are equal, then we know that we can't use that value again, so I set match to true . If they are not equal, then if it was already disqualified, I ignore it and set match back to false , otherwise, return that value.

There are more elegant ways to do this; this is just the simplest.

I propose the following code:

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
int arr[5] = { 1,2,3,2,1 };

int N = sizeof(arr) / sizeof(arr[0]);
sort(arr, arr + N);
int index = -1;

// Check first element is different comparing with second one. 
// If the array is only one element, then first element is unique
if (N == 1 || arr[0] != arr[1]) {
    index = 0;
}
else {
    int i = 1;
    while (i < N - 1 && index == -1) {
        // Check number is different to previous and different to next value
        if (arr[i - 1] != arr[i] && arr[i] != arr[i + 1]) {
            index = i;
        }
        else
        {
            i++;
        }
    }
    if (index == -1) {
        // No found, check last element comparing with previous
        if (arr[i - 1] != arr[i]) {
            index = i;
        }
    }
}

if (index != -1) {
    // Have found min value
    cout << "Min not repeated value is " << arr[index] << endl;
}
else {
    // All elements are repeated
    cout << "No min value" << endl;
}
}

Once the array is sorted, I compare each value with previous and next value to check it is unique. But for first and last element are a special case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM