Skip to main content
Commonmark migration
Source Link

Input: {2, 3, 7, 6, 8, -1, -10, 15}   Output: 1

 

Input: { 2, 3, -7, 6, 8, 1, -10, 15 }  Output: 4

 

Input: {1, 1, 0, -1, -2}                Output: 2

Input: {2, 3, 7, 6, 8, -1, -10, 15}   Output: 1

 

Input: { 2, 3, -7, 6, 8, 1, -10, 15 }  Output: 4

 

Input: {1, 1, 0, -1, -2}                Output: 2

Input: {2, 3, 7, 6, 8, -1, -10, 15}   Output: 1

Input: { 2, 3, -7, 6, 8, 1, -10, 15 }  Output: 4

Input: {1, 1, 0, -1, -2}                Output: 2

Tweeted twitter.com/StackCodeReview/status/1065666243032686592
edited tags
Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191
 Input:  {2, 3, 7, 6, 8, -1, -10, 15}
 Output: 1

 Input:  { 2, 3, -7, 6, 8, 1, -10, 15 }
 Output: 4

 Input: {1, 1, 0, -1, -2}
 Output: 2

Input: {2, 3, 7, 6, 8, -1, -10, 15}   Output: 1

Input: { 2, 3, -7, 6, 8, 1, -10, 15 }  Output: 4

Input: {1, 1, 0, -1, -2}                Output: 2

using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ArrayQuestions 
{

[TestClass]
public class FindTheSmallestNonnegativeNumberNoyInArray
{
    [TestMethod]
    public void FindTheSmallestNonnegativeNumberNoyInArrayTest()
    {
        int[] array = {2, 3, 7, 6, 8, -1, -10, 15};
        int result = FindSmallestPositiveHash(array);
        int expected = 1;
        Assert.AreEqual(expected, result);
    }
   

   [TestMethod]
    public void FindTheSmallestNonnegativeNumberNoyInArrayTest2()
    {
        int[] array = { 2, 3, -7, 6, 8, 1, -10, 15 };
        int result = FindSmallestPositiveHash(array);
        int expected = 4;
        Assert.AreEqual(expected, result);
    }
    private int FindSmallestPositiveHash(int[] array)
    {
        HashSet<int> set = new HashSet<int>();
        int maxPositive = 0;
        for (int i = 0; i < array.Length; i++)
        {
            if (!set.Contains(array[i]))
            {
                maxPositive = Math.Max(maxPositive, array[i]);
                set.Add(array[i]);
            }
        }
        for (int i = 1; i < maxPositive; i++)
        {
            if (!set.Contains(i))
            {
                return i;
            }
        }
        return maxPositive + 1;
    }
}
}

}

 Input:  {2, 3, 7, 6, 8, -1, -10, 15}
 Output: 1

 Input:  { 2, 3, -7, 6, 8, 1, -10, 15 }
 Output: 4

 Input: {1, 1, 0, -1, -2}
 Output: 2
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ArrayQuestions 
{

[TestClass]
public class FindTheSmallestNonnegativeNumberNoyInArray
{
    [TestMethod]
    public void FindTheSmallestNonnegativeNumberNoyInArrayTest()
    {
        int[] array = {2, 3, 7, 6, 8, -1, -10, 15};
        int result = FindSmallestPositiveHash(array);
        int expected = 1;
        Assert.AreEqual(expected, result);
    }
   

   [TestMethod]
    public void FindTheSmallestNonnegativeNumberNoyInArrayTest2()
    {
        int[] array = { 2, 3, -7, 6, 8, 1, -10, 15 };
        int result = FindSmallestPositiveHash(array);
        int expected = 4;
        Assert.AreEqual(expected, result);
    }
    private int FindSmallestPositiveHash(int[] array)
    {
        HashSet<int> set = new HashSet<int>();
        int maxPositive = 0;
        for (int i = 0; i < array.Length; i++)
        {
            if (!set.Contains(array[i]))
            {
                maxPositive = Math.Max(maxPositive, array[i]);
                set.Add(array[i]);
            }
        }
        for (int i = 1; i < maxPositive; i++)
        {
            if (!set.Contains(i))
            {
                return i;
            }
        }
        return maxPositive + 1;
    }
}

}

Input: {2, 3, 7, 6, 8, -1, -10, 15}   Output: 1

Input: { 2, 3, -7, 6, 8, 1, -10, 15 }  Output: 4

Input: {1, 1, 0, -1, -2}                Output: 2

using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ArrayQuestions 
{

[TestClass]
public class FindTheSmallestNonnegativeNumberNoyInArray
{
    [TestMethod]
    public void FindTheSmallestNonnegativeNumberNoyInArrayTest()
    {
        int[] array = {2, 3, 7, 6, 8, -1, -10, 15};
        int result = FindSmallestPositiveHash(array);
        int expected = 1;
        Assert.AreEqual(expected, result);
    }
   

   [TestMethod]
    public void FindTheSmallestNonnegativeNumberNoyInArrayTest2()
    {
        int[] array = { 2, 3, -7, 6, 8, 1, -10, 15 };
        int result = FindSmallestPositiveHash(array);
        int expected = 4;
        Assert.AreEqual(expected, result);
    }
    private int FindSmallestPositiveHash(int[] array)
    {
        HashSet<int> set = new HashSet<int>();
        int maxPositive = 0;
        for (int i = 0; i < array.Length; i++)
        {
            if (!set.Contains(array[i]))
            {
                maxPositive = Math.Max(maxPositive, array[i]);
                set.Add(array[i]);
            }
        }
        for (int i = 1; i < maxPositive; i++)
        {
            if (!set.Contains(i))
            {
                return i;
            }
        }
        return maxPositive + 1;
    }
}
}
removed language tag from title
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191
Loading
Source Link
Gilad
  • 5.4k
  • 5
  • 38
  • 65
Loading