Given an Array, find all the elements occurring odd number of time. All the elements are

That is all elements are positive integers.

Code

public static void FindOddOccurrence(int[] input) { int length = input.Length; Console.Write("input Array is " + string.Join(",", input)); Console.Write(Environment.NewLine); Console.WriteLine("Elements Occurring odd number of Times"); for (int i = 0; i < length; i++) { input[Math.Abs(input[i])] = -input[Math.Abs(input[i])]; } for (int i = 0; i < length; i++) { if (input[Math.Abs(input[i])] < 0) { Console.Write(Math.Abs(input[i]) + ","); input[Math.Abs(input[i])] = -input[Math.Abs(input[i])]; } } Console.Write(Environment.NewLine); }

Test

Input Array is 1,2,3,1,3,1,6

Elements Occurring odd number of Times

1,2,6,

Input Array is 1,2,3,2,3,1,3

Elements Occurring odd number of Times

3,

Input Array is 1,2,4,6,7,8,3,2,3,1,3,4,1,3,2,2,2,2,2,4,8,3

Elements Occurring odd number of Times

1,2,4,6,7,3,

Input Array is 1,2,3,4,5,6,7,1,2,3,4,5,6,7

Elements Occurring odd number of Times

No Comments

Filed under:
Array