在数组中查找唯一的整数

时间:2014-10-15 11:05:26

标签: java arrays logic

我有一个像这样的整数数组: A = {1,1,4,4,4,1,1}

我想计算每个数字一次,对于这个例子,awnser是2因为我想要计数1次和4次 我不想使用排序方法 我无法找到使用java解决它的方法。 我做了这个,但它给了我0

    public static void main(String args[]) {
    int a[] = { 1,1,4,4,4,4,1,1};
    System.out.print(new Test4().uniques(a));
}

public int uniques(int[] a) {
    int unique = 0;
    int tempcount = 0;

    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a.length; j++) {
            if (a[i] == a[j]) {
                tempcount++;
            }
        }
        if (tempcount <= 2) {
            unique=a[i];
        }
        tempcount = 0;
    }
    return unique;
}

问题的目的是理解它的逻辑但不使用现成的方法或类来解决它

5 个答案:

答案 0 :(得分:2)

这个应该有效。我想这可能不是最优雅的方式,但它非常简单,只使用简单的数组。方法返回数组中的位数,但不计算重复数 - 我相信这是你的目标。

public int uniques(int[] a) {
    int tempArray[] = new int[a.length];
    boolean duplicate = false;
    int index = 0;
    int digitsAdded = 0;

    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < tempArray.length; j++) {
            if (a[i] == tempArray[j]) {
                duplicate = true;
            }
        }
        if(!duplicate) {
            tempArray[index] = a[i];
            index++;
            digitsAdded++;
        }
        duplicate = false;
    }
    //this loop is needed if you have '0' in your input array - when creating temp 
    //array it is filled with 0s and then any 0 in input is treated as a duplicate
    //again - not most elegant solution, maybe I will find better later...
    for(int i = 0; i < a.length; i++) {
        if(a[i] == 0) {
            digitsAdded++;
            break;
        }
    }
    return digitsAdded;
}

答案 1 :(得分:1)

如果只限于数组,请考虑尝试:

让我们拿一个相同大小的orignal数组的临时数组,我们存储每个独特的字母,并假设a是你的orignal数组,

int[] tempArray= new int[a.length];
int tempArraycounter = 0;
bool isUnique = true;
for (int i = 0; i < a.length; i++)
{
    isUnique = true;
    for (int j = 0; j < tempArray.length; j++)
    {
        if(tempArray[j] == a[i])
            isUnique = false;
    }
    if(isUnique)
    {
       tempArray[tempArraycounter] = a[i];
       tempArraycounter++;
       isUnique = false;
    }
}

现在tempArraycounter将是您的答案;)

答案 2 :(得分:1)

首先,在您的解决方案中,您将返回int unique,即您将其设置为唯一a[i]的值。所以它只会在你的例子中返回1或4。

接下来,关于实际的解决方案。您需要检查您是否已经看过该号码。您需要检查的是,阵列中的每个数字只出现在您的位置前面而不是之前。您可以使用以下代码执行此操作。

public int uniques(int[] a) {
    int unique = 1;
    boolean seen = false;

    for (int i = 1; i < a.length; i++) {
        for (int j = 0; j < i; j++) {
            if (a[i] == a[j]) {
                seen = true;
            }
        }
        if (!seen) {
            unique++;
        }
        seen = false;
    }
    return unique;
}

在此代码中,您将迭代您看到的数字并与您正在检查的数字进行比较(a[i])。你知道,因为它是独特的,你以前看不到它。

答案 3 :(得分:1)

我看到两种可能的解决方案:

  • 使用set

    public int unique(int[] a) {
        Set<Integer> set = new HashSet<>();
        for (int i : a) {
            set.add(i);
        }
        return set.size();
    }
    
  • 使用快速排序

    public int unique(int[] a) {
        Arrays.sort(a);
        int cnt = 1;
        int example = a[0];
        for (int i = 1; i < a.length; i++) {
            if (example != a[i]) {
                cnt++;
                example = a[i];
            }
        }
        return cnt;
    }
    

我的表现测试表明第二种解决方案更快〜30%。

答案 4 :(得分:0)

尝试以下代码:

int test[]={1,1,4,4,4,1,1};
Set<Integer> set=new LinkedHashSet<Integer>();
for(int i=0;i<test.length;i++){
    set.add(test[i]);
}
System.out.println(set);

输出

[1, 4]

最后set将包含唯一的integers