forked from xieqilu/Qilu-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path38.DuplicateNumsArray.cs
54 lines (50 loc) · 1.38 KB
/
38.DuplicateNumsArray.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Find duplicate elements in an array
//value of elements is positive and between 0 to n-1, n is the size of array
//all duplicate elements cannot occur more than twice in the array
//O(n) time and O(1) space
using System;
using System.Collections.Generic;
using System.Collections;
namespace FindDuplicateInArray
{
class Finder
{
public static void FindDuplicate(int[] arr) //O(n) time, O(1) space
{ //elements value cannot be 0
// duplicate elements cannot occur more than twice
int n = arr.Length;
foreach (int i in arr) {
if (arr [Math.Abs (i)] > 0)
arr [Math.Abs (i)] = -arr [Math.Abs (i)];
else if (arr [Math.Abs (i)] < 0)
Console.Write (Math.Abs(i) + " ");
}
}
public static void FindTest(int[] arr) // O(n) time, O(n) space
{ // arr can have elements value equals to 0
// duplicate elements can occur more than twice
int n = arr.Length;
List<int> temp = new List<int> ();
for (int i = 0; i < n; i++)
temp.Add (0);
foreach (int i in arr) {
if (temp [i] == 0)
temp [i]++;
else if (temp [i] == 1) {
Console.Write (i + " ");
temp [i]++;
}
}
}
}
class MainClass
{
public static void Main (string[] args)
{
int[] test = new int[7] {1, 1, 1, 6, 6, 4, 6};
Finder.FindTest (test);
Console.WriteLine (" ");
Finder.FindDuplicate (test);
}
}
}