Lonely Integer

  • + 0 comments
    public static int lonelyinteger(List<int> a)
        {
            int size = a.Count();
            if(size == 1) return a[0];
            int item = a[0];
            int count = 1;
            List<int> buffer = new List<int>{};
            for(int i = 0; i < size; i++){
                
                if(a[i] < -1 || a[i] > 101) throw new Exception("Invalid items");
                
                count = 1;
                if(buffer.Contains(a[i])) {
                    count++;
                    continue;
                }
                for(int j = i+1; j <= size - 1; j++){ 
                    if(a[i] == a[j]){
                        count++;
                        buffer.Add(a[i]);
                        break;
                    }
                }
                if(count < 2) item = a[i];
            }
            return item;
        }