forked from YearOfProgramming/2017Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Go challenge 3 (YearOfProgramming#427)
* Add solution for max occurance challenge * Add test cases * Add README.md * Move code to correct location
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
## Single Number | ||
#### To run the code : | ||
* Open this directory in terminal and run `go run max_occurance.go` to compile and run the code. | ||
* To test the code just run, `go test` in same directory. | ||
``` | ||
$ go run max_occurance.go | ||
gGiven array is : [2 a l 3 l 4 k 2 3 4 a 6 c 4 m 6 m k 9 10 9 8 7 8 10 7] | ||
Element that occur only max number of times : 4 | ||
$ go test | ||
PASS | ||
ok path/to/2017Challenges/challenge_2/rust/makernaren 0.005s | ||
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Take an array of mixed data types and find the element that occurs | ||
// most number of times. | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Finds the maximum occurance, takes in interface as input and output params | ||
//as elements in the given array may be of any data type | ||
func FindMajority(input []interface{}) (result interface{}) { | ||
// Create a hashmap, where element of that array is key and its number of | ||
// occurances is the value. | ||
occurances := make(map[interface{}]int) | ||
for _, key := range input { | ||
occurances[key]++ | ||
} | ||
// Return when we find the max occurance. | ||
max := 0 | ||
kv_pair := make([]interface{}, 2) | ||
for k, v := range occurances { | ||
// Replace the variable until you find max | ||
if v > max { | ||
max = v | ||
kv_pair[0] = k | ||
kv_pair[1] = v | ||
} | ||
} | ||
return kv_pair[0] | ||
} | ||
|
||
func main() { | ||
given := []interface{}{2, "a", "l", 3, "l", 4, "k", 2, 3, 4, "a", 6, "c", | ||
4, "m", 6, "m", "k", 9, 10, 9, 8, 7, 8, 10, 7} | ||
fmt.Printf("Given array is : %v \n", given) | ||
result := FindMajority(given) | ||
fmt.Printf("Element that occur only max number of times : %v \n", result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// max_occurance_test | ||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestIntegers(t *testing.T) { | ||
input := []interface{}{2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6} | ||
output := 4 | ||
result := FindMajority(input) | ||
if result != output { | ||
t.Error("for", input, | ||
"expected", output, | ||
"got", result) | ||
} | ||
} | ||
|
||
func TestStrings(t *testing.T) { | ||
input := []interface{}{"a", "a", "b", "b", "b", "b", "c", "c", "c", "c", | ||
"c", "d", "d", "e"} | ||
output := "c" | ||
result := FindMajority(input) | ||
if result != output { | ||
t.Error("for", input, | ||
"expected", output, | ||
"got", result) | ||
} | ||
} | ||
|
||
func TestMultipleTypes(t *testing.T) { | ||
input := []interface{}{2, "a", "l", 3, "l", 4, "k", 2, 3, 4, "a", 6, "c", | ||
4, "m", 6, "m", "k", 9, 10, 9, 8, 7, 8, 10, 7, 7, 7, 7, 7, 7, 7, 7, 7} | ||
output := 7 | ||
result := FindMajority(input) | ||
if result != output { | ||
t.Error("for", input, | ||
"expected", output, | ||
"got", result) | ||
} | ||
} |