September 2006 - Posts
using System;
System;namespace FindMaxValue{
class Program
FindMaxValue{
class Program
class Program{
static void Main(string[] args){
int[] myArray = new int[] {22,10,4,6,33,7,8,9,11};Console.Write(MaxValue.FindMax(myArray));Console.ReadLine();}
}
class MaxValue
static void Main(string[] args){
int[] myArray = new int[] {22,10,4,6,33,7,8,9,11};Console.Write(MaxValue.FindMax(myArray));Console.ReadLine();}
}
class MaxValue
int[] myArray = new int[] {22,10,4,6,33,7,8,9,11};Console.Write(MaxValue.FindMax(myArray));Console.ReadLine();}
}
class MaxValue
Console.Write(MaxValue.FindMax(myArray));Console.ReadLine();}
}
class MaxValue
Console.ReadLine();}
}
class MaxValue
class MaxValue{
public static int FindMax(int[] array){
int maxValue = array[0];for(int i=1; i<array.Length; i++){
if (array[i] > maxValue)maxValue = array[i];
}
return maxValue;}
}
}
public static int FindMax(int[] array){
int maxValue = array[0];for(int i=1; i<array.Length; i++){
if (array[i] > maxValue)maxValue = array[i];
}
return maxValue;}
}
}
int maxValue = array[0];for(int i=1; i<array.Length; i++){
if (array[i] > maxValue)maxValue = array[i];
}
return maxValue;}
}
}
for(int i=1; i<array.Length; i++){
if (array[i] > maxValue)maxValue = array[i];
}
return maxValue;}
}
}
if (array[i] > maxValue)maxValue = array[i];
}
return maxValue;}
}
}
return maxValue;}
}
}
using System;
namespace BubbleSort
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[] {2,5,7,1,9,0,3,4,2,6,7,5};
// Before sorting
for (int i = 0; i < array.Length - 1; i++)
{
Console.Write(array[i]);
}
Console.ReadLine();
ArraySort.SortArray(array);
// After sorting
for (int i = 0; i < array.Length - 1; i++)
{
Console.Write(array[i]);
}
Console.ReadLine();
}
}
class ArraySort
{
public static void SortArray(int[] numbers)
{
int tempInt;
for (int i = (numbers.Length-1); i >=0; i--)
{
for (int j =1; j <=i; j++)
{
if (numbers[j-1] > numbers[j])
{
tempInt = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = tempInt;
}
}
}
}
}
}
Reverse an Array is one of the most common programming algorithm questions that is asked in many tech interviews. Here is a quick way with sample code:
using System;
System; namespace ReverseArray
{
/// <summary>
/// Reverse an Array quickly
/// </summary>
ReverseArray
{
/// <summary>
/// Reverse an Array quickly
/// </summary> class Program
{
static void Main(string[] args)
{
int[] array = new int[] {1,2,3,4,5,6,7,8,9};
// Print before reverse
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i]);
}
Console.ReadLine();
class Program
{
static void Main(string[] args)
{
int[] array = new int[] {1,2,3,4,5,6,7,8,9};
// Print before reverse
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i]);
}
Console.ReadLine();
ReverseArrayClass.ReverseArray(array);
// Print after reverse
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i]);
}
Console.ReadLine();
}
}
/// <summary>
/// Array Reverse Alogorithm
/// </summary>
class ReverseArrayClass
{
public static void ReverseArray(int[] array)
{
int tempInt;
for (int i=0; i<(array.Length/2); i++)
{
tempInt = array[i];
array[i] = array[array.Length -i-1];
array[array.Length -i-1] = tempInt;
}
}
}
}
class ReverseArrayClass
{
public static void ReverseArray(int[] array)
{
int tempInt;
for (int i=0; i<(array.Length/2); i++)
{
tempInt = array[i];
array[i] = array[array.Length -i-1];
array[array.Length -i-1] = tempInt;
}
}
}
}
I had a screening interview last week where an SDE Lead asked me what type of collection object I should be using for a given situation. Well, there was not a straight and short answer to it. But, I mentioned a few points to consider and here are they:
- Array is the fastest among all collections available in C# unless you need sort, search and dynamically extension of array size.
- ArrayList is good for storing custom object types, frequent data change, frequent insert/delete operation.
- SortedList is for fast object retrieval using an index or a key. Avoid using SortedList for large data changes
- Queue is for first in first out.
- Stack is for last in first out
- StringCollection is for storing strings.
- NameValueCollection is to store strings of key-value pairs in a presorted order.This is used for data that changes frequently where you need to insert/delete items regularly and where you need to cache items for fast retrieval.
- ListDictionary to store small amounts of data usually fewer than 10 items.
- Hashtable is used to store a large number of records and store data that may not change frequently.
- HybridDictionary is to store frequently queried data when you expect the number of records to be low with occasional increases in size.
These are the general thumb rules for choosing a collection type, but it depends on actual situation in which you need to design.
I had to remove some dups from one of the tables in my database. Well, there is a couple of ways to do that. They are: using distinct, using derived table, correlated subqueries and using dymanic sql. Each of these technique has its own pros and cons. Well, I prefer using correlated subqueries over to others bases on the scenario that I have. It is always debatable and based on the actual situation and usage.
Here is what I did for my table.
CREATE TABLE DuplicateRecords (
TABLE DuplicateRecords (Id INT IDENTITY,
INT IDENTITY,Name VARCHAR(50))
Name VARCHAR(50))
GO
INSERT DuplicateRecords (Name)
DuplicateRecords (Name)SELECT 'Sanjeeb Sarangi' UNION ALL
SELECT 'Sanjeeb Sarangi' UNION ALLSELECT 'Atoshi Sahoo' UNION ALL
SELECT 'Atoshi Sahoo' UNION ALLSELECT 'Jim Boone' UNION ALL
SELECT 'Jim Boone' UNION ALLSELECT 'Trevor Carnahan' UNION ALL
SELECT 'Trevor Carnahan' UNION ALLSELECT 'Atoshi Sahoo' UNION ALL
SELECT 'Atoshi Sahoo' UNION ALLSELECT 'Sanjeeb Sarangi' UNION ALL
SELECT 'Sanjeeb Sarangi' UNION ALLSELECT 'Chad Justice' UNION ALL
SELECT 'Chad Justice' UNION ALLSELECT 'Harpo Marx' UNION ALL
SELECT 'Harpo Marx' UNION ALLSELECT 'Atoshi Sahoo' UNION ALL
SELECT 'Atoshi Sahoo' UNION ALLSELECT 'Trevor Carnahan' UNION ALL
SELECT 'Trevor Carnahan' UNION ALLSELECT 'Sanjeeb Sarangi' UNION ALL
SELECT 'Sanjeeb Sarangi' UNION ALLSELECT 'Chad Justice' UNION ALL
SELECT 'Chad Justice' UNION ALLSELECT 'Zeppo Marx' UNION ALL
SELECT 'Zeppo Marx' UNION ALLSELECT 'Sanjeeb Sarangi' UNION ALL
SELECT 'Sanjeeb Sarangi' UNION ALLSELECT 'Vinay Raturi' UNION ALL
SELECT 'Vinay Raturi' UNION ALLSELECT 'Daniel Neely' UNION ALL
SELECT 'Daniel Neely' UNION ALLSELECT 'Vinay Raturi'
SELECT 'Vinay Raturi'SELECT * FROM DuplicateRecords
* FROM DuplicateRecordsDELETE DuplicateRecords
DuplicateRecordsWHERE Id > (
Id > (SELECT MIN(Id)
SELECT MIN(Id)FROM DuplicateRecords dupsWHERE dups.Name = DuplicateRecords.Name
FROM DuplicateRecords dupsWHERE dups.Name = DuplicateRecords.Name
WHERE dups.Name = DuplicateRecords.Name)
SELECT * FROM DuplicateRecords
* FROM DuplicateRecordsDROP TABLE DuplicateRecords
TABLE DuplicateRecords
More Posts