Check Occurrences in java
You will be given an array and you need to find the whether there is any element occurring thrice. If yes, then print 'True' else print 'False'
Input Format -
You will be taking a number as an input from STDIN which tells about the length of the array. On another line, array elements should be there with single space between them.
Output Format -
You need to print the Boolean value.
Sample Test Case:
Sample Input:-12
9 8 7 7 8 6 5 4 7 8 1 2
Sample Output -
True
Explanation:
Elements 8 and 7 occurring Thrice and hence printing 'True'.
Input Format -
You will be taking a number as an input from STDIN which tells about the length of the array. On another line, array elements should be there with single space between them.
Output Format -
You need to print the Boolean value.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* You will be given an array and you need to find the whether there is any element occurring thrice. | |
* If yes, then print 'True' else print 'False' | |
*/ | |
import java.io.*; | |
import java.util.*; | |
public class CandidateCode { | |
public static void main(String args[] ) throws Exception { | |
Scanner sc=new Scanner(System.in); | |
int size; | |
size=sc.nextInt(); | |
int [] arr=new int [size]; | |
for(int i=0;i<size;i++){ | |
arr[i]=sc.nextInt(); | |
} | |
boolean isExist=false; | |
int count=0; | |
for(int a=0;a<arr.length;a++){ | |
int x=arr[a]; | |
for(int b=0;b<arr.length;b++){ | |
int y=arr[b]; | |
if(x==y){ | |
count++; | |
} | |
} | |
if(count>2){ | |
isExist=true; | |
} | |
} | |
System.out.println(isExist?"True":"False"); | |
sc.close(); | |
} | |
} |
Sample Input:-12
9 8 7 7 8 6 5 4 7 8 1 2
Sample Output -
True
Explanation:
Elements 8 and 7 occurring Thrice and hence printing 'True'.
Comments
Post a Comment