如何在C ++中找到数组的最小和唯一元素?

我有一个大小为N的数字数组。我需要找到唯一的最小元素,因此如果arr[5] ={1,2,3,1,2}的答案是3。 我用以下代码尝试过:

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,j);
        }
    }
}

但是问题是这只有在我有2个相同的arr元素时才有效。我可以创建条件是否相同的数目,但是我可以有3个或4个或1000个,所以这很奇怪。那么,什么更愚蠢的方式做到这一点呢?预先谢谢你。

weiweisenge 回答:如何在C ++中找到数组的最小和唯一元素?

尝试此代码,它使用无序映射

int m = 2147483647;
    int int_array[] = { 1,2,3,1,6,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);
,

一种实现方法是使用unordered_map存储每个元素的频率。在这里,存储的{ key,value }对将是{ the value of the element,frequency of the element in the array }。一旦存储它们,就可以对unordered_map中存储的所有元素进行一次迭代,以找到频率为1的最小元素。

,

我提出以下代码:

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
int arr[5] = { 1,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;
}
}

对数组排序后,我将每个值与上一个和下一个值进行比较以检查其唯一性。但是对于第一个和最后一个元素来说是一个特例。

,

对数组进行排序后,您可以计算复制对象的数量,如果该成员是唯一的,则其计数为零:

int main()
{
    int arr[] = { 1,4,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;
}
,

使用我在评论中所说的,再加上一个布尔值。

int arr[] = { 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

如果两个值相等,那么我们知道不能再次使用该值,因此我将match设置为true。如果它们不相等,那么如果它已经不符合条件,我将忽略它并将match设置回false,否则,返回该值。

有更优雅的方法可以做到这一点;这是最简单的。

本文链接:https://www.f2er.com/3086234.html

大家都在问