using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text.RegularExpressions;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//This part assumes that all the elements in the array lie in the range (min,min + arraylength -1).
int[] arr1 = { 1, 2, 3, 4, 4, 5, 2, 1, 1, 1 };
int count = 1;
int min = arr1.Min();
int max = arr1.Max();
for (int i = 0; i < arr1.GetLength(0); i++)
{
arr1[Math.Abs(arr1[i])] *= -1;
}
for (int i = 0; i < arr1.GetLength(0); i++)
{
Console.Write(arr1[i] + " ");
}
Console.WriteLine();
for (int i = min; i <= max; i++) { if (arr1[i] > 0)
{
Console.Write(i + " ");
}
}
Console.WriteLine();
//This part sorts the array and finds the frequency map and counts the frequency of each element.
int[] arr2 = { 1, 2, 3, 4, 4, 5, 2, 1, 1, 1 };
Array.Sort(arr2);
for (int i = 0; i < arr2.GetLength(0) - 1; i++)
{
count = 1;
while (arr2[i + 1] == arr2[i])
{
i++;
count++;
}
if (count % 2 == 0)
{
Console.WriteLine(arr2[i] + " " + count);
}
}
}
}
}
A blog on programming and software design interview problems and solutions. Also there are some application development related stuff.
Pages
Monday, July 23, 2012
Given an array of positive integers, print out all the numbers which are repeated an even number of times without using additional storage
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment