程序以查找数组中最不常见的条目

时间:2018-07-17 16:53:08

标签: java arrays

我正在为数组中最少出现的元素编写代码,由于某种原因,我的逻辑出错了,编译器只打印了数组中的第一个元素还是第二个元素?有人知道怎么了吗?

package javaapplication10;
import java.util.*;

public class JavaApplication10 {
    public static void main(String[] args) {
        int  m =1000;
        int count = 0;
        int store = 0;
        int c = 0;
        Scanner scan = new Scanner(System.in);
        int[] a = new int[20] ;
        int n;
        System.out.print("Enter no of elements");
        n = scan.nextInt();
        for(int i =0; i<n;i++) {
            a[i] = scan.nextInt();
        }
        for(int i =0; i <n ; i++) {  
            c = a[i] ; 
            for(int j =0; j <n ; j++) { 
                if(a[j] ==c) {
                    count++ ;
                }
                if(j == (n-1)) {
                    if(count<m ) {
                    store = a[i];
                    m = count;
                   }
                }
                count = 0;
            }
       }
       System.out.print(store);
    }
}

4 个答案:

答案 0 :(得分:1)

更好的解决方案是进行排序。我们首先对数组进行排序,然后线性遍历数组。

static int leastFrequent(int arr[], int n)
// n is length of array
    {

        // Sort the array
        Arrays.sort(arr);

        // find the min frequency using 
        // linear traversal
        int min_count = n+1, res = -1;
        int curr_count = 1;

        for (int i = 1; i < n; i++) {
            if (arr[i] == arr[i - 1])
                curr_count++;
            else {
                if (curr_count < min_count) {
                    min_count = curr_count;
                    res = arr[i - 1];
                }

                curr_count = 1;
            }
        }

        // If last element is least frequent
        if (curr_count < min_count)
        {
            min_count = curr_count;
            res = arr[n - 1];
        }

        return res;
    }

答案 1 :(得分:0)

我想您正在尝试实现以下逻辑

  1. 查找数组中每个元素的计数
  2. 如果找到计数较低的元素,请存储该元素
  3. 重复数组中的每个元素-查找最低的元素。

count应该在内循环的结尾休息,

package javaapplication10;
import java.util.*;

public class JavaApplication10 {
    public static void main(String[] args) {
        int  m =1000;
        int count = 0;
        int store = 0;
        int c = 0;
        Scanner scan = new Scanner(System.in);
        int[] a = new int[20] ;
        int n;
        System.out.print("Enter no of elements");
        n = scan.nextInt();
        for(int i =0; i<n;i++) {
            a[i] = scan.nextInt();
        }
        for(int i =0; i <n ; i++) {  
            c = a[i] ; 
            for(int j =0; j <n ; j++) { 
                if(a[j] ==c) {
                    count++ ;
                }
                if(j == (n-1)) {
                    if(count<m ) {
                    store = a[i];
                    m = count;
                   }
                }
            }
         count = 0;
       }
       System.out.print(store);
    }
}

答案 2 :(得分:0)

您只有一个计数器,因此一旦从一个元素过渡到另一个元素,您将失去此计数。

您可以持有计数器的辅助映射并随时进行更新,但是坦率地说,使用Java的流将为您节省很多样板代码:

int leastOccuring = 
    Arrays.stream(a)
          .boxed()
          .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())
          .entrySet()
          .stream()
          .min(Map.Entry.comparingByValue())
          .map(Map.Entry::geyKey)
          .get();

答案 3 :(得分:0)

这将是您程序的正确代码。

import java.util.*;

public class LeastOccuringElementInArray {

    public static void main(String[] args) {

        int  m = 0;
        int count = 0;
        int store = 0;
        int c = 0;
       Scanner scan = new Scanner(System.in);
        int[] a = new int[20] ;
       int n;
       System.out.print("Enter no of elements");
       n = scan.nextInt();

       for(int i =0; i<n;i++)
       {
           a[i] = scan.nextInt();
       }
       for(int i =0; i <n ; i++)
        {  c = a[i] ; 
               for(int j =0; j <n ; j++)
                {
                       if(a[j] == c)
                       {
                           count++ ;

                       }
                       if(j == (n-1))
                       {
                            if(m!=0 && m > count)
                            {
                                  store = a[i];
                                  m = count;

                            }
                            else {
                                m = count;
                            }
                        }

             }

               count = 0;

    }
                System.out.print(store);
                scan.close();
    } 
}