Home > Net >  Another way to compare two values of an array List to find the lonely integer in Java?
Another way to compare two values of an array List to find the lonely integer in Java?

Time:02-18

The problem to solve is to find a non-repeating integer in an Integer list. I used a while loop to compare each value with another. The function, however, returns the wrong value. This is a Hackerrank challenge which can be found here .

public static int lonelyinteger(List<Integer> a) {

int lonelyInt = 0;
for (int i = 0; i < a.size(); i  ) {
    for (int j=1;j<a.size();j  ){
        while (a.get(i)!=a.get(j)){
            lonelyInt =a.get(i);
            
        }
    }
}
return lonelyInt;
}

I would really appreciate any hints and/or more straightforward solutions. I am new to Java and practicing my skills.

CodePudding user response:

Your approach of comparing each number with all the other numbers is correct in principle.

If we compare the ith element of the array with all the other elements, and none are equal to it, then we know it is the unique number.

Your program is not doing this correctly.

Think of the loops like this:

  • The outer loop using the index i is giving us the number we are checking for loneliness, a.get(i).
  • The inner loop using the index j is giving us each number to check against the current candidate selected by the outer loop, a.get(j).

So for each iteration of the outer loop we will need to keep track of whether any iteration of the inner loop matched. We could use a local boolean named equalNumberFound, which we set to false at the start of each iteration of the outer loop.

In the inner loop, we check whether we've found an equal number, and if we have, set it to true. Make sure that you don't check a number against itself!

At the end of each iteration of the outer loop, we check equalNumberFound, and if it's still false we can return the current outer loop number, because we now know that no other number was equal.

You need to review what you know about while loops, as it seems you have some incorrect assumptions about how they behave.

CodePudding user response:

One of the ways you could approach the problem is by sorting the list first then compare each element to its adjacent element since the elements are limited to occurring either once or twice. Since only one element occurs once, then the element without an identical adjacent element is the stop condition. This approach achieves O(log(N)) time complexity and O(1) space complexity.

public static int lonelyInteger(List<Integer> a) {
    Collections.sort(a);

    for (int i = 1; i < a.size(); i  = 2) {
        if (a.get(i - 1).intValue() != a.get(i).intValue()) return a.get(i - 1);
    }
    return a.get(a.size()-1);
}

CodePudding user response:

You can make use of hashset. Iterate through the list of integers:

  1. Add element to hashset, if integer is not present in the hashset.
  2. Remove the integer element, if it is already present in the hashset

Now return the lone non-repeating integer in the hashset.

public static int lonelyinteger(List<Integer> a) {
    
     Set<Integer> set = new HashSet<>();
          
     for (int n : a) {        
         if (set.contains(n)) {
            set.remove(n);
         } else {
            set.add(n);
         }        
     }
     return set.stream().findFirst().get();
}
  • Related