Lonely Integer

Sort by

recency

|

943 Discussions

|

  • + 0 comments

    Python solution, more basic programming

    def lonelyinteger(a):
        # Write your code here
        x = 0
        for i in a:
            for n in a:
                if n == i:
                    x += 1
            if x != 1:
                x = 0
            else:
                return(i)
    
  • + 0 comments
    public static int lonelyinteger(List<Integer> a) {
            // Sort list
            Collections.sort(a);
            //   Return first elem if size is one or second elem doesn't match
            int ans=a.get(0);
            if(a.size()==1 || a.get(0) != a.get(1))
                    return ans;
            // Iterate through list
            for(int i=1;i<a.size()-1;i++) {
            // Compare num before and after from index 1 - size-1
                    if(a.get(i)!= a.get(i-1) && a.get(i)!= a.get(i+1)){
            // If unequal return elem
                            return a.get(i);                
                    }
            }
            // If each elem has match but last return last
            return a.get(a.size()-1);            
    }
    
  • + 0 comments

    List< Integer> passedNumbers = new ArrayList<>(); for (int i = 0; i a.size(); i++) { if(!passedNumbers.contains(a.get(i))) { passedNumbers.add(a.get(i)); } else { passedNumbers.remove(a.get(i)); } } return passedNumbers.get(0)

  • + 0 comments

    JAVA

    for(Integer n: a){ int frec = Collections.frequency(a, n); if(frec==1){ num=n; } } return num;

  • + 0 comments

    Scala Solution

    def lonelyinteger(a: Array[Int]): Int = {
        // Write your code here)
            a.reduce(_^_)
        }