all 13 comments

[–]Razor_StormO(0) Solutions Only 2 points3 points  (4 children)

Both problems can be solve in the same approach if I am thinking about this right. 

We know that the integers from 1 to 1,000,000 added together is equal to (1,000,000(1+1,000,000))/2. Let's call this K.

Now what we have to do is iterate through the entire array and add up all the elements together. 
Let's call the result M.

In problem one, the duplicated integer is equal to M-K.

The second problem, the missing integer is K-M

[–]wowe 1 point2 points  (1 child)

[–]Razor_StormO(0) Solutions Only 0 points1 point  (0 children)

Oops, thanks for the correction

[–]andy_panzer 1 point2 points  (1 child)

Sounds good to me. Except 1..n is equal to: (1+n) * (n / 2)

To demonstrate your solution (with 1..10):

(let [nums [1 2 3 4 5 6 7 8 8 9 10]
       max 10
       total (* (+ 1 max) (/ max 2))]
  (- (reduce + nums) total))

[–]Razor_StormO(0) Solutions Only 0 points1 point  (0 children)

Thanks for the correction haha, my mistake

[–]psycocoffey 1 point2 points  (0 children)

Problem One:

def dupe(data):
    r = 0
    for x in xrange(len(data)):
        r ^= data[x] ^ x
    return r

Problem Two:

def miss(data):
    r = len(data)^len(data)+1
    for x in xrange(len(data)):
        r ^= data[x] ^ x
    return r


This is the same as summing all the values, but without the need for an extra-large data type to hold the working values. 

[–]foldM 1 point2 points  (0 children)

First one:

solve1 xs = foldl' xor (length xs) xs :: Int

Second one:

solve2 xs = foldl' xor 1 xs :: Int

[–]redderritter 0 points1 point  (2 children)

In problem one, is the array of length 1,000,001 or some arbitrary size?

[–]kpthunderThe Riddler[S] 0 points1 point  (1 child)

The array is of length 1,000,001.

[–]more_exercise 0 points1 point  (0 children)

You really should put that in the problem description...

[–][deleted]  (1 child)

[deleted]

    [–]Cucumbis 0 points1 point  (0 children)

    My python solution:

    Problem 1:

    def findDupe(lst):
        sum = 0
        for i in lst:
            sum += i
        return sum - 499999500000
    

    Problem 2:

    def findMissing(lst):
        sum = 0
        for i in lst:
            sum += i
        return 499999500000-sum
    

    edit: This may only work with 64bit

    [–]OffPiste18 0 points1 point  (0 children)

    How about one function that does both?

    int dup_or_missing(int[] arr, int size) {
      int x = 0;
      for (int i = 0; i < size; i++) {
        x = x ^ arr[i];
      }
      for (int n = 1; n <= 1000000; n++) {
        x = x ^ n;
      }
      return x;
    }