string match sequence array - java

public class sequence {
public static void main(String args[]){
char[] c = {'a','x','c','e'};
char[] t = {'x','b'};
int count = 0,j;
for(int i=0;i<(c.length);i++)
{
int p = i;
int x = 0;
for( j=0;j<(t.length);j++){
if(c[p]!=c[j]){
break;
}
else
x++;
System.out.print(x);
if(x==((t.length))){
count++;
}
p++;
}
System.out.print('a');
}
System.out.println("Number of Occurences " + count);
}
}
My task is to count the number of time the sequence ie t[] occurs in the mother array c[].
I am not able to get the required result even though i tries all the iterations in my mind where it worked well.I am kind of a starter in programming so need some help here.
Thankya!

You should take this piece of code:
if(x==((t.length))){
count++;
}
From the inner loop.

The problem is that your x == t.length check is inside your inner for loop, but your inner for loop will never let x reach t.length. Also, your x variable is redundant and is always equal to j so it can be removed.
To fix this, move your length-check to the outside of the loop.
Edit: Also, you're accessing the wrong array in your inner loop (where the break statement is).
public static void main(String args[]){
char[] c = {'a','x','c','e'};
char[] t = {'x','b'};
int count = 0, j;
for (int i = 0; i < (c.length); i++) {
int p = i;
for (j = 0; j < (t.length); j++){
if (c[p] != t[j]) {
break;
}
p++;
}
if (j == t.length){
count++;
}
}
System.out.println("Number of Occurences " + count);
}

You shouldn't need two for loops:
public static void main(String args[]){
char[] c = {'a','x','b','c','x','b','e'};
char[] t = {'x','b'};
int count = 0;
int tInd = 0;
for(int i=0;i<(c.length);i++)
{
if(tInd < t.length && t[tInd] == c[i]){ // Found a member of t[]
tInd ++;
} else {
tInd = 0; // Didn't find t[]
}
if(tInd == t.length){ // Found full t[] sequence
count++;
tInd = 0;
}
}
System.out.println("Number of Occurences " + count);
}

You don't need to loop on all c elem (you can stop in the position of last possible match). In the inner loop as soon as you find a match you must continue on next c elem:
public static void main(String[] args) {
char[] c = {'c','a','x','b'};
char[] t = {'x','b'};
int count = 0, j;
for(int i=0;i<=(c.length-t.length);i++)
{
for(j=0;j<(t.length);j++){
if(t[j]!=c[i+j]){
break;
}
}
if(j==t.length)
count++;
}
System.out.println("Number of Occurences " + count);
}

Related

Vowel counter Java application: stuck in for loops? Doesn't seem to be an infinite loop

I made a java program to count the number of vowels in a string, and I need to use a, or multiple, for loop(s) to do it for a project. The problem is that it does not do anything, or just takes too long, after inputting the string:
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter a string to count the vowels>> ");
String x = s.nextLine();
int numVowels = countVowels(x, "aeiou");
System.out.println(numVowels);
}
static int countVowels(String x, String vowels)
{
int count = 0;
String z = x.toLowerCase();
for (int i = 0; i <= vowels.length() - 1; i++)
{
if (i == vowels.length() - 1)
{
for (int n = z.indexOf(vowels.substring(i)); n != -1; count++)
{
z.replace(vowels.substring(i), "");
}
}
else if (z.indexOf(vowels.substring(i, i + 1)) != -1)
{
for (int n = z.indexOf(vowels.substring(i, i + 1)); n != -1; count++)
{
z.replace(vowels.substring(i, i + 1), "");
}
}
}
return count;
}
}
I have reduced the number of loops, because the original was very confusing. I think the problem is with the nested loops, but I have not yet tried running this on a local compiler, only online IDEs. I've heard that it makes a world of difference for compile times.
It's an infinite loop: you're not doing anything with z if you use only z.replace. Since strings are immutable in Java, you can't change it by only calling a method, you must assign it again:
z = z.replace(...)
I took the liberty of using while-loop instead of for-loop since the idea is to keep looping till find, count and replace all the vowels one by one.
static int countVowels(String x, String vowels) {
int count = 0;
String z = x.toLowerCase();
for (int i = 0; i < vowels.length(); i++) {
String vowel = Character.toString(vowels.charAt(i));
while (z.indexOf(vowel) != -1) {
count++;
z = z.replaceFirst(vowel, "");
}
}
return count;
}

finding first even element and outputting index

My program:
public static void evenval (int[] array ){
int even=0;
for (int r = 0; r < array.length; r++) {
while (r == array[r]) {
if (array[r] % 2 ==0) {
even = array[r];
System.out.println("The first even number's index is:"+array[r]);
}
I'm trying to make a loop where it finds the first even number in an array and get it to output it's index to the main method.
I'm stuck, please help.
Try to use just for loop like this:
int even = 0;
for (int r=0;r<array.length;r++){
if (array[r] % 2 ==0){
even = r;
break;
}
}
System.out.println("The first even number's index is:"+ even );
Using the break; when find the even number.
For example if you want to find 100th even number use this:
int even = 0;
int count = 0;
for (int r=0;r<array.length;r++){
if(array[r] % 2 ==0){
if(count !=100){
count++;
}else{
even = r;
break;
}
}
}
System.out.println("The first even number's index is:"+ even );
It's better to create a small method in your class and call it from main to do this for you and return the index or -1 if a valid even number is not found. :
public class EvenTest{
int getFirstEvenIndex(int[] array){
for (int r=0; r < array.length; r++){
if (array[r] % 2 ==0){
return r;
}
}
return -1;
}
public static void main(String... args){
int[] arr = [3,5,1,2,7,8];
EvenTest et = new EvenTest();
System.out.println("First even is at index: " + et.getFirstEvenIndex(arr));
}
}
class test {
public static void main(String[] args) {
int[] array = {5, 7, 1, 8, 9, 3,110};
int evenCount =2;//required count ....for 100th even put evenCount = 100;
evenval(array,evenCount);
}
public static void evenval(int[] array,int evenCount) {
for (int i=0;i<array.length;i++) {
if(array[i] % 2 == 0&&--evenCount==0) {
System.out.println("the required even number is "+array[i]+" at index "+i);
return;
}
}
}
}
Just remove while statement in your code.
int even = 0;
for (int r = 0; r < array.length; r++) {
if (array[r] % 2 == 0) {
even = array[r];
System.out.println("The first even number's index is:" + array[r]);
}
}
If you want to stop after finding first even number, just put break inside if statement
int even = 0;
for (int r = 0; r < array.length; r++) {
if (array[r] % 2 == 0) {
even = array[r];
System.out.println("The first even number's index is:" + array[r]);
break;
}
}
You could simply use a bitwise operation as well (performance wise, it's quicker):
for (int n : array) {
if ((n&1) == 0) {
System.out.println("The first even number's index is:" + n);
break;
}
}

Generating 10 random numbers without duplicate with fundamental techniques

my intend is to use simplest java (array and loops) to generate random numbers without duplicate...but the output turns out to be 10 repeating numbers, and I cannot figure out why.
Here is my code:
int[] number = new int[10];
int count = 0;
int num;
while (count < number.length) {
num = r.nextInt(21);
boolean repeat = false;
do {
for (int i=0; i<number.length; i++) {
if (num == number[i]) {
repeat = true;
} else if (num != number[i] && i == count) {
number[count] = num;
count++;
repeat = true;
}
}
} while (!repeat);
}
for (int j = 0; j < number.length; j++) {
System.out.print(number[j] + " ");
}
How about you use a Set instead? If you also want to keep track of the order of insertion you can use a LinkedHashSet.
Random r = new Random();
Set<Integer> uniqueNumbers = new HashSet<>();
while (uniqueNumbers.size()<10){
uniqueNumbers.add(r.nextInt(21));
}
for (Integer i : uniqueNumbers){
System.out.print(i+" ");
}
A Set in java is like an Array or an ArrayList except it handles duplicates for you. It will only add the Integer to the set if it doesn't already exist in the set. The class Set has similar methods to the Array that you can utilize. For example Set.size() is equivalent to the Array.length and Set.add(Integer) is semi-equivalent to Array[index] = value. Sets do not keep track of insertion order so they do not have an index. It is a very powerful tool in Java once you learn about it. ;)
Hope this helps!
You need to break out of the for loop if either of the conditions are met.
int[] number = new int[10];
int count=0;
int num;
Random r = new Random();
while(count<number.length){
num = r.nextInt(21);
boolean repeat=false;
do{
for(int i=0; i<number.length; i++){
if(num==number[i]){
repeat=true;
break;
}
else if(i==count){
number[count]=num;
count++;
repeat=true;
break;
}
}
}while(!repeat);
}
for(int j=0;j<number.length;j++){
System.out.print(number[j]+" ");
}
This will make YOUR code work but #gonzo proposed a better solution.
Your code will break the while loop under the condition: num == number[i].
This means that if the pseudo-generated number is equal to that positions value (the default int in java is 0), then the code will end execution.
On the second conditional, the expression num != number[i] is always true (otherwise the code would have entered the previous if), but, on the first run, when i == count (or i=0, and count=0) the repeat=true breaks the loop, and nothing else would happen, rendering the output something such as
0 0 0 0 0 0...
Try this:
int[] number = new int[10];
java.util.Random r = new java.util.Random();
for(int i=0; i<number.length; i++){
boolean repeat=false;
do{
repeat=false;
int num = r.nextInt(21);
for(int j=0; j<number.length; j++){
if(number[j]==num){
repeat=true;
}
}
if(!repeat) number[i]=num;
}while(repeat);
}
for (int k = 0; k < number.length; k++) {
System.out.print(number[k] + " ");
}
System.out.println();
Test it here.
I believe the problem is much easier to solve. You could use a List to check if the number has been generated or not (uniqueness). Here is a working block of code.
int count=0;
int num;
Random r = new Random();
List<Integer> numbers = new ArrayList<Integer>();
while (count<10) {
num = r.nextInt(21);
if(!numbers.contains(num) ) {
numbers.add(num);
count++;
}
}
for(int j=0;j<10;j++){
System.out.print(numbers.get(j)+" ");
}
}
Let's start with the most simple approach, putting 10 random - potentially duplicated - numbers into an array:
public class NonUniqueRandoms
{
public static void main(String[] args)
{
int[] number = new int[10];
int count = 0;
while (count < number.length) {
// Use ThreadLocalRandom so this is a contained compilable unit
number[count++] = ThreadLocalRandom.current().nextInt(21);
}
for (int j = 0; j < number.length; j++) {
System.out.println(number[j]);
}
}
}
So that gets you most of the way there, the only thing you know have to do is pick a number and check your array:
public class UniqueRandoms
{
public static void main(String[] args)
{
int[] number = new int[10];
int count = 0;
while (count < number.length) {
// Use ThreadLocalRandom so this is a contained compilable unit
int candidate = ThreadLocalRandom.current().nextInt(21);
// Is candidate in our array already?
boolean exists = false;
for (int i = 0; i < count; i++) {
if (number[i] == candidate) {
exists = true;
break;
}
}
// We didn't find it, so we're good to add it to the array
if (!exists) {
number[count++] = candidate;
}
}
for (int j = 0; j < number.length; j++) {
System.out.println(number[j]);
}
}
}
The problem is with your inner 'for' loop. Once the program finds a unique integer, it adds the integer to the array and then increments the count. On the next loop iteration, the new integer will be added again because (num != number[i] && i == count), eventually filling up the array with the same integer. The for loop needs to exit after adding the unique integer the first time.
But if we look at the construction more deeply, we see that the inner for loop is entirely unnecessary.
See the code below.
import java.util.*;
public class RandomDemo {
public static void main( String args[] ){
// create random object
Random r = new Random();
int[] number = new int[10];
int count = 0;
int num;
while (count < number.length) {
num = r.nextInt(21);
boolean repeat = false;
int i=0;
do {
if (num == number[i]) {
repeat = true;
} else if (num != number[i] && i == count) {
number[count] = num;
count++;
repeat = true;
}
i++;
} while (!repeat && i < number.length);
}
for (int j = 0; j < number.length; j++) {
System.out.print(number[j] + " ");
}
}
}
This would be my approach.
import java.util.Random;
public class uniquerandom {
public static void main(String[] args) {
Random rnd = new Random();
int qask[]=new int[10];
int it,i,t=0,in,flag;
for(it=0;;it++)
{
i=rnd.nextInt(11);
flag=0;
for(in=0;in<qask.length;in++)
{
if(i==qask[in])
{
flag=1;
break;
}
}
if(flag!=1)
{
qask[t++]=i;
}
if(t==10)
break;
}
for(it=0;it<qask.length;it++)
System.out.println(qask[it]);
}}
public String pickStringElement(ArrayList list, int... howMany) {
int counter = howMany.length > 0 ? howMany[0] : 1;
String returnString = "";
ArrayList previousVal = new ArrayList()
for (int i = 1; i <= counter; i++) {
Random rand = new Random()
for(int j=1; j <=list.size(); j++){
int newRand = rand.nextInt(list.size())
if (!previousVal.contains(newRand)){
previousVal.add(newRand)
returnString = returnString + (i>1 ? ", " + list.get(newRand) :list.get(newRand))
break
}
}
}
return returnString;
}
Create simple method and call it where you require-
private List<Integer> q_list = new ArrayList<>(); //declare list integer type
private void checkList(int size)
{
position = getRandom(list.size()); //generating random value less than size
if(q_list.contains(position)) { // check if list contains position
checkList(size); /// if it contains call checkList method again
}
else
{
q_list.add(position); // else add the position in the list
playAnimation(tv_questions, 0, list.get(position).getQuestion()); // task you want to perform after getting value
}
}
for getting random value this method is being called-
public static int getRandom(int max){
return (int) (Math.random()*max);
}

How to use conditional statement to check whether array equal to a row of matrix?

I have an array of int type, I want check that if all elements of this array are equal to the elements of i-th row of m×n matrix. For example:
array={5,0,2,3};
and
matrix={{1,3,2,2}{5,0,2,3}{2,1,2,9}};
so if
array[j] = matrix[i][j];
for all j=0,1,2 then
print("the wanted i is: "+i);
I have written this code:
public static void main(String[] args){
int i, j, m=3, n=4;
int[][] A0 = {{0,2,2,5},{2,0,3,1},{0,2,0,5}};
int[] A={0,2,0,5};
int seq = 0;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(A[j] != A0[i][j]){
break;
}
seq= i;
}
}
System.out.print(seq);
}
But its not work well because it always continue checking until final row.
Is there is any other better idea? Many thanks.
First of all,
if any value is different in a row,
then you want to skip to the next iteration of the outer loop.
You can do that by giving the outer loop a label,
and the continue statement.
Secondly,
as soon as you found a row where all values match,
you can break out of the outer loop.
OUT: for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (A[j] != A0[i][j]) {
continue OUT;
}
}
seq = i;
break;
}
By using Arrays.equals you can simplify by quite a lot,
by eliminating the inner loop,
and with it all the label + continue black magic:
for (i = 0; i < m; i++) {
if (Arrays.equals(A, A0[i])) {
seq = i;
break;
}
}
You have two forconditions. So when you reach
if(A[j] != A0[i][j]) {
break;
}
You will break the only this for loop, so you will continue the other loop:
for(i=0;i<m;i++) {
So you have to break this one too, here is a solution
boolean canBreak = false;
for(i=0;i<m;i++) {
for(j=0;j<n;j++) {
if(A[j] != A0[i][j]) {
canBreak = true;
break;
}
if (canBreak) break;
seq= i;
}
}
You can simply use Arrays.equals()
private int[][] matrix;
public boolean equalsRow(int[] row, int index) {
return Arrays.equals(row, matrix[index]);
}
private int[][] myArray;
public boolean equalsRow(int[] row, int x)
{
return Arrays.equals(row, myArray[x]);
}
simpler
public static void main(String[] args){
int i, j, m=3, n=4;int ct=0;
int[][] A0 = {{0,2,2,5},{2,0,3,1},{0,2,0,5}};
int[] A={0,2,0,5};
int seq = 0;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(A[j] == A0[i][j]){
ct++;
}
else
{
break;
}
}
if(ct==4)
{
seq=i;
break;
}
ct=0;
}
System.out.print(seq);
}

Finding non duplicate element in an array

I am stuck in the following program:
I have an input integer array which has only one non duplicate number, say {1,1,3,2,3}. The output should show the non duplicate element i.e. 2.
So far I did the following:
public class Solution {
public int singleNumber(int[] arr){
int size = arr.length;
int temp = 0;
int result = 0;
boolean flag = true;
int[] arr1 = new int[size];
for(int i=0;i<size;i++){
temp = arr[i];
for(int j=0;j<size;j++){
if(temp == arr[j]){
if(i != j)
//System.out.println("Match found for "+temp);
flag = false;
break;
}
}
}
return result;
}
public static void main(String[] args) {
int[] a = {1,1,3,2,3};
Solution sol = new Solution();
System.out.println("SINGLE NUMBER : "+sol.singleNumber(a));
}
}
Restricting the solution in array is preferable. Avoid using collections,maps.
public class NonRepeatingElement {
public static void main(String[] args) {
int result =0;
int []arr={3,4,5,3,4,5,6};
for(int i:arr)
{
result ^=i;
}
System.out.println("Result is "+result);
}
}
Since this is almost certainly a learning exercise, and because you are very close to completing it right, here are the things that you need to change to make it work:
Move the declaration of flag inside the outer loop - the flag needs to be set to true every iteration of the outer loop, and it is not used anywhere outside the outer loop.
Check the flag when the inner loop completes - if the flag remains true, you have found a unique number; return it.
From Above here is the none duplicated example in Apple swift 2.0
func noneDuplicated(){
let arr = [1,4,3,7,3]
let size = arr.count
var temp = 0
for i in 0..<size{
var flag = true
temp = arr[i]
for j in 0..<size{
if(temp == arr[j]){
if(i != j){
flag = false
break
}
}
}
if(flag == true){
print(temp + " ,")
}
}
}
// output : 1 , 4 ,7
// this will print each none duplicated
/// for duplicate array
static void duplicateItem(int[] a){
/*
You can sort the array before you compare
*/
int temp =0;
for(int i=0; i<a.length;i++){
for(int j=0; j<a.length;j++){
if(a[i]<a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
int count=0;
for(int j=0;j<a.length;j++) {
for(int k =j+1;k<a.length;k++) {
if(a[j] == a[k]) {
count++;
}
}
if(count==1){
System.out.println(a[j]);
}
count = 0;
}
}
/*
for array of non duplicate elements in array just change int k=j+1; to int k = 0; in for loop
*/
static void NonDuplicateItem(int[] a){
/*
You can sort the array before you compare
*/
int temp =0;
for(int i=0; i<a.length;i++){
for(int j=0; j<a.length;j++){
if(a[i]<a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
int count=0;
for(int j=0;j<a.length;j++) {
for(int k =0 ;k<a.length;k++) {
if(a[j] == a[k]) {
count++;
}
}
if(count==1){
System.out.println(a[j]);
}
count = 0;
}
}
public class DuplicateItem {
public static void main (String []args){
int[] a = {1,1,1,2,2,3,6,5,3,6,7,8};
duplicateItem(a);
NonDuplicateItem(a);
}
/// for first non repeating element in array ///
static void FirstNonDuplicateItem(int[] a){
/*
You can sort the array before you compare
*/
int temp =0;
for(int i=0; i<a.length;i++){
for(int j=0; j<a.length;j++){
if(a[i]<a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
int count=0;
for(int j=0;j<a.length;j++) {
//int k;
for(int k =0; k<a.length;k++) {
if(a[j] == a[k]) {
count++;
}
}
if(count==1){
System.out.println(a[j]);
break;
}
count = 0;
}
}
public class NonDuplicateItem {
public static void main (String []args){
int[] a = {1,1,1,2,2,3,6,5,3,6,7,8};
FirstNonDuplicateItem(a);
}
I have a unique answer, it basically takes the current number that you have in the outer for loop for the array and times it by itself (basically the number to the power of 2). Then it goes through and every time it sees the number isn't equal to double itself test if its at the end of the array for the inner for loop, it is then a unique number, where as if it ever find a number equal to itself it then skips to the end of the inner for loop since we already know after one the number is not unique.
public class Solution {
public int singleNumber(int[] arr){
int size = arr.length;
int temp = 0;
int result = 0;
int temp2 = 0;
int temp3 = 0;
boolean flag = true;
int[] arr1 = new int[size];
for(int i=0;i<size;i++){
temp = arr[i];
temp2 = temp*temp;
for(int j=0;j<size;j++){
temp3 = temp*arr[j];
if(temp2==temp3 && i!=j)
j=arr.length
if(temp2 != temp3 && j==arr.length){
//System.out.println("Match found for "+temp);
flag = false;
result = temp;
break;
}
}
}
return result;
}
public static void main(String[] args) {
int[] a = {1,1,3,2,3};
Solution sol = new Solution();
System.out.println("SINGLE NUMBER : "+sol.singleNumber(a));
}
}
not tested but should work
public class Solution {
public int singleNumber(int[] arr){
int size = arr.length;
int temp = 0;
int result = 0;
boolean flag = true;
int[] arr1 = new int[size];
for(int i=0;i<size;i++){
temp = arr[i];
int count=0;
for(int j=0;j<size;j++){
if(temp == arr[j]){
count++;
}
}
if (count==1){
result=temp;
break;
}
}
return result;
}
Try:
public class Answer{
public static void main(String[] args) {
int[] a = {1,1,3,2,3};
int[] b =new int[a.length];
//instead of a.length initialize it to maximum element value in a; to avoid
//ArrayIndexOutOfBoundsException
for(int i=0;i<a.length;i++){
int x=a[i];
b[x]++;
}
for(int i=0;i<b.length;i++){
if(b[i]==1){
System.out.println(i); // outputs 2
break;
}
}
}
}
PS: I'm really new to java i usually code in C.
Thanks #dasblinkenlight...followed your method
public class Solution {
public int singleNumber(int[] arr){
int size = arr.length;
int temp = 0;
int result = 0;
int[] arr1 = new int[size];
for(int i=0;i<size;i++){
boolean flag = true;
temp = arr[i];
for(int j=0;j<size;j++){
if(temp == arr[j]){
if(i != j){
// System.out.println("Match found for "+temp);
flag = false;
break;
}
}
}
if(flag == true)
result = temp;
}
return result;
}
public static void main(String[] args) {
int[] a = {1,1,3,2,3};
Solution sol = new Solution();
System.out.println("SINGLE NUMBER : "+sol.singleNumber(a));
}
}
One disastrous mistake was not enclosing the content of if(i != j) inside braces. Thanks all for your answers.
If you are coding for learning then you can solve it with still more efficiently.
Sort the given array using merge sort of Quick Sort.
Running time will be nlogn.
The idea is to use Binary Search.
Till required element found All elements have first occurrence at even index (0, 2, ..) and next occurrence at odd index (1, 3, …).
After the required element have first occurrence at odd index and next occurrence at even index.
Using above observation you can solve :
a) Find the middle index, say ‘mid’.
b) If ‘mid’ is even, then compare arr[mid] and arr[mid + 1]. If both are same, then the required element after ‘mid’ else before mid.
c) If ‘mid’ is odd, then compare arr[mid] and arr[mid – 1]. If both are same, then the required element after ‘mid’ else before mid.
Another simple way to do so..
public static void main(String[] art) {
int a[] = { 11, 2, 3, 1,1, 6, 2, 5, 8, 3, 2, 11, 8, 4, 6 ,5};
Arrays.sort(a);
System.out.println(Arrays.toString(a));
for (int j = 0; j < a.length; j++) {
if(j==0) {
if(a[j]!=a[j+1]) {
System.out.println("The unique number is :"+a[j]);
}
}else
if(j==a.length-1) {
if(a[j]!=a[j-1]) {
System.out.println("The unique number is :"+a[j]);
}
}else
if(a[j]!=a[j+1] && a[j]!=a[j-1]) {
System.out.println("The unique number is :"+a[j]);
}
}
}
Happy Coding..
Using multiple loops the time complexity is O(n^2), So the effective way to resolve this using HashMap which in the time complexity of O(n). Please find my answer below,
`public static int nonRepeatedNumber(int[] A) {
Map<Integer, Integer> countMap = new HashMap<>();
int result = -1;
for (int i : A) {
if (!countMap.containsKey(i)) {
countMap.put(i, 1);
} else {
countMap.put(i, countMap.get(i) + 1);
}
}
Optional<Entry<Integer, Integer>> optionalEntry = countMap.entrySet().stream()
.filter(e -> e.getValue() == 1).findFirst();
return optionalEntry.isPresent() ? optionalEntry.get().getKey() : -1;
}
}`

Categories

Resources