简体   繁体   中英

How to check whether the key number is repeated in the array?

How to get the repeated number from the key(b) my program is like this a user enter 166456 and key = 6 then the output must be like 6 is repeated 3 times in the array please also show me if this can be done without array
I am even getting error for int cannot be to int[]

    int []a,i,j,count=0,b=0,n;
    Scanner sc=new Scanner(System.in);
    n=sc.nextInt(System.in);
    int []a=new int[n];
    for(i=0;i<n;++i)
    {
        a[i]=sc.nextInt();

    }
    System.out.println("Which nuber would you like to find:");
    b=sc.nextInt();
    for(j=0;j<n;++j)
    {
        if(a[i]==b)
        {
            ++count;
        }
        else
        {
            System.out.println("Not found");
        }
    }
    System.out.println("No of time "+b+" is repeated "+count);

You are doing some wrong variable declaration. If you declare an array as int []a then it will consider all variables as an array. That's why you are getting the error. Either you can declare as an int a[] or declare other variables on a different line.

Please refer below code,

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        int []a;
        int b=0, count=0;
        int i, j, n;
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        a=new int[n];
        for(i = 0; i < n; ++i)
        {
            a[i]=sc.nextInt();

        }
        System.out.println("Which nuber would you like to find:");
        b=sc.nextInt();
        for(j=0;j<n;++j)
        {
            if(a[j]==b)
            {
                ++count;
            }
            else
            {
                System.out.println("Not found");
            }
        }
        System.out.println("No of time "+b+" is repeated "+count);

    }

}

Output

6
1
6
6
4
5
6
Which nuber would you like to find:
6
Not found
Not found
Not found
No of time 6 is repeated 3

As far as I have understood the requirement, You need help regarding finding out the total frequency of the digit from the user's input. (Assuming digits will be only from 0 to 9. Correct me If I am wrong on this assumption).

So, for this, we can just use the integer array of size 10 to store each digit's frequency. For example, consider the input of total 6 digits - "166456". Our integer array's value will be (from index 0 to 9) 0100113000. So, we can directly return the index of the digit we want to search, in this example return value of array[6] which is 3.

    int[] num=new int[10]; // storing each digit's frequency
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt(); // count for total digits
    for(int i=0;i<n;i++){
        int index = sc.nextInt();
        num[index]++;
    }
    System.out.println("Which number you would like to find out?");
    int b = sc.nextInt();
    if(num[b]!=0)
        System.out.println("No. of time "+b+" is repeated "+num[b]);
    else
        System.out.println("Not found");

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