Can someone help me compare my solution(below) to the binary search posted by others?


  • 0
    G

    I'm assuming that the binary search method is faster. I still wanted to know if my solution is O(logN) or better or worse.

    public class Solution {
        public int singleNonDuplicate(int[] nums) {
            int temp = 0;
            for(int i = 0; i < nums.length; i++) {
                if(i >= nums.length-1) {
                    temp = nums[nums.length-1];
                    break;
                }
                if(nums[i] == nums[i+1]) {
                    i++;
                } else {
                    temp = nums[i];
                    break;
                }
            }
            return temp;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.